mysqli

Mysqli select from from two tables

三世轮回 提交于 2021-02-11 12:29:31
问题 I have data stored in two different tables called "posts" and "comments". Now i'm using two mysqli_query. $q = mysqli_query($db,"SELECT * FROM posts WHERE username='$username'"); $q = mysqli_query($db,"SELECT * FROM comments WHERE username='$username'"); Can I make this with only one mysqli_query or with 3 tables? 回答1: You can simply join both tables: $q = mysqli_query($db,"SELECT * FROM posts LEFT JOIN comments ON comments.username=posts.username WHERE comments.username='$username'");

JSON can't returning cyrillic data

丶灬走出姿态 提交于 2021-02-11 10:05:25
问题 $connect = mysqli_connect("localhost", "root", "123456", "json_test"); $sql = "SELECT * FROM names"; $query = mysqli_query( $connect,$sql ); while( $row=mysqli_fetch_array($query) ){ $arr[]=array( 'id' => $row['id'], 'title' => $row['name'] ); } echo json_encode( $arr ); is returning ??????? and it's cyrillic text(utf-8). How to fix it? 回答1: The mysqli_set_charset() function specifies the default character set to be used when sending data from and to the database server. You need to add

JSON can't returning cyrillic data

旧巷老猫 提交于 2021-02-11 10:05:16
问题 $connect = mysqli_connect("localhost", "root", "123456", "json_test"); $sql = "SELECT * FROM names"; $query = mysqli_query( $connect,$sql ); while( $row=mysqli_fetch_array($query) ){ $arr[]=array( 'id' => $row['id'], 'title' => $row['name'] ); } echo json_encode( $arr ); is returning ??????? and it's cyrillic text(utf-8). How to fix it? 回答1: The mysqli_set_charset() function specifies the default character set to be used when sending data from and to the database server. You need to add

With PHP and mysqli, is it possible to catch all mysqli errors in one place across all PHP scripts?

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-11 06:31:26
问题 Is it possible to catch all errors for mysqli calls in one place with one statement or do I have to check each mysqli call individually for an error? I want to do something along the lines of: if(mysqli_error_occurs_anywhere_in_web_app) { die('<html><head></head><body>database error</body></html>'); } basically if a mysqli error occurs anywhere I want to output an html database error document. is this possible? 回答1: You could alternately wrap all your queries on your own query function,

PHP Commands out of sync; you can't run this command now

回眸只為那壹抹淺笑 提交于 2021-02-10 23:42:19
问题 I've seen this question before but all the solutions do not work for me. The main solution is to store the result which I already do. Most people say I am running 2 simultaneous queries which I don't understand. I may have used this function below more than once but I always free and close the statement so not sure why this is happening. You can most likely ignore the top half of the function which just generates a string to represent the types. This is the full DB class I made: (I edited it

PHP Commands out of sync; you can't run this command now

允我心安 提交于 2021-02-10 23:42:19
问题 I've seen this question before but all the solutions do not work for me. The main solution is to store the result which I already do. Most people say I am running 2 simultaneous queries which I don't understand. I may have used this function below more than once but I always free and close the statement so not sure why this is happening. You can most likely ignore the top half of the function which just generates a string to represent the types. This is the full DB class I made: (I edited it

PHP Commands out of sync; you can't run this command now

好久不见. 提交于 2021-02-10 23:41:54
问题 I've seen this question before but all the solutions do not work for me. The main solution is to store the result which I already do. Most people say I am running 2 simultaneous queries which I don't understand. I may have used this function below more than once but I always free and close the statement so not sure why this is happening. You can most likely ignore the top half of the function which just generates a string to represent the types. This is the full DB class I made: (I edited it

How to check if element exists in database and return a warning message?

落爺英雄遲暮 提交于 2021-02-10 19:52:26
问题 I have a case where I am importing a spreadsheet of items, and when inserting I need to check for each item if already exists (either by serial_no or serial_imei) and then for each that EXISTS I need to inform the user when the submission is done with a message such as: "Item with serial no 123456 was already found in database". Here is the way I am inserting: $stmt = $db->prepare("INSERT INTO devices (serial_imei,serial_no) VALUES (?,?)"); for ($i = 0; $i < $min; $i++) { $stmt->bind_param(

Connection of MySQL with PHP not working

陌路散爱 提交于 2021-02-10 07:26:08
问题 This is my situation: I'm trying to connect to my MySQL database with a PHP file on my Apache server, now: my PHP can connect to the MySQL database when I run it from the terminal (using "php -f file.php" ) but when I execute it from a web page it just doesn't connect. This is my php file: echo "TRY CONNECTION"; $conn = mysql_connect($servername, $username, $password); echo "EXECUTED CONNECTION"; The Linux shell prints this: TRY CONNECTIONEXECUTED CONNECTION But the web page prints this: :TRY

php and mysql, best practices

大城市里の小女人 提交于 2021-02-09 05:42:26
问题 I started working with php and mysql today. Basically, what I have, is an empty page with pieces that I fill in from looking up an id in a database. So on my home page I have an url that looks like this: <a href="content/display.php?id=id1"> And then in my display.php I have this: <?php include '../includes/header.php'; $id = $_GET['id']; $mysqli = new mysqli('localhost','username','password','dbname'); if($result = $mysqli->query("SELECT * FROM portfolio WHERE id='".$id."'")) { while($row =