This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
How can I prevent SQL injection in PHP?
(27 answers)
Closed 2 years ago.
Expected result:
Loop through all entries in the checkedout table, and select the entry from the game table where the barcode field is the same.
Actual behaviour / issue:
For the most part, this is working as intended. If I set the barcode field to a numerical value in the game table, and then "checkout" that barcode, everything works as intended. The barcodes I'll be using are in the format of ABC12345678. Once I change the values in the barcode field, in the game table to the alphanumeric version, it no longer runs the secondary select statement and displays this error: Fatal error: Call to a member function fetch_assoc() on boolean which refers to the following line: while ($row2 = $result2->fetch_assoc()) {
Oddly enough, if I run the exact same select statement SELECT * FROM game WHERE barcode = 'ABC12345678' on the MySQL instance, it returns the proper results.
Question
Do I need to be using a different method to select based on the value now being alphanumeric? Do I need to manipulate the data in some way?
Code:
$sql = "SELECT * FROM checkedout";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$userid = $row["userid"];
$barcode = $row["barcode"];
echo "$userid </br>";
echo "$barcode </br>";
$sql2 = "SELECT * FROM game WHERE barcode = " . $barcode . "";
$result2 = $conn->query($sql2);
while ($row2 = $result2->fetch_assoc()) {
$title = $row2["title"];
$console = $row2["console"];
echo "$title </br>";
echo "$console </br>";
}
checkedout table:
game table:
Related
This question already has answers here:
MySQL select 10 random rows from 600K rows fast
(28 answers)
Closed 4 years ago.
i have a table named quiz ,it have six columns like id,question,option1,option2,option3,answer.i want to loop through all the values by using the following query
$sql = "SELECT id, option1, option2,option3,answer,question FROM quiz";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - question: " . $row["question"]. " " .
$row["option1"]. "<br>";
}
How can i get random values from mysql table everytime i want only 4 values.I am new to development .
welcome to Stackoverflow!
Use this;
$sql = "SELECT id, option1, option2,option3,answer,question FROM quiz ORDER BY RAND() LIMIT 4";
This will get rows 100% randomly! But, it can get you duplicates..
This question already has answers here:
Get sum of MySQL column in PHP
(9 answers)
Closed 1 year ago.
I'm trying to get the sum off all the prize money from a column in a MySql table, but I'm not getting a result.
$result = mysqli_query("SELECT SUM(prize_money) FROM cards");
while ($rows = mysqli_fetch_array($result)) {
echo $rows['SUM(prize_money)'];
}
I just want to add all of the numbers in the prize_money column then echo the results.
Thank You
You should apply an alias to the SUM so it is easier to access in the PHP.
You then need to pass the connection string to the mysqli_query function as the first parameter.
So for example if your database connection were:
$con=mysqli_connect("localhost","my_user","my_password","my_db");
then you'd use this code to execute the query and assign the alias:
$result = mysqli_query($con, 'SELECT SUM(prize_money) AS sum_prize_money FROM cards');
$row = mysqli_fetch_assoc($result);
$sum = $row['sum_prize_money'];
echo $sum;
Do you get a result if you do this:
$result = mysqli_query("SELECT SUM(prize_money) FROM cards");
$rows = mysqli_fetch_array($result);
echo $rows
As good practice, you should learn to aliase your sql variables e.g. SUM(prize_money) AS total etc
This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
Ok, hi. So i was trying to create an Android App wich uses a MySQL database to generate a RecylerView. To get the Data i use a PhP file store on the same Server. But i wanted to extend the App a bit. And now if i call this Line: $query = 'SELECT * FROM items WHERE catid = "$catid"'; the result is empty.
The weird Thing is: If i enter the Query in PhPMyAdmin it shows me the correct Results.
Here is the Complete PHP-File (Database Login removed) (Annnd the mandatory disclaimer: I'm not fluent in English, so please excuse some typos c:)
EDIT: I think some People missunderstood: The Problem is not in Android.. I'm sure. And the "generate".. just ignore that i didnt know what other Word i could use.. So the Problem is only in the PHP File
<?php
$connection = mysqli_connect("","","","");
$type = $_GET["t"];
if($type == "categorys"){
$query = "SELECT * FROM categorys";
}else{
$catid = $_GET["id"];
$query = 'SELECT * FROM items WHERE catid = "$catid"';
}
$result = mysqli_query($connection,$query);
while ($row = mysqli_fetch_assoc($result)) {
$array[] = $row;
}
header('Content-Type:Application/json');
echo json_encode($array);
?>
Write it like this, with double-quotes:
$query = "SELECT * FROM items WHERE catid = $catid";
If catid is a string, then:
$query = "SELECT * FROM items WHERE catid = '$catid'";
Anyway, you should use prepared statement.
Try this:
$query = "SELECT * FROM items WHERE catid = '$catid'";
or:
$query = "SELECT * FROM items WHERE catid = '".$catid."'";
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 6 years ago.
Hi I know other questions already have the answer to this but I don't understand. I've done lots of research and am still confused.
I have a table in my Mysql that I made with PHP My Admin it has three rows (I'll add to these) and 3 columns (id, name and price) I'd like to output the first rows name and id but I keep getting the error
Trying to get property of non-object;
<?php
$db = new PDO('mysql:host=localhost;dbname=sitefiles;charset=utf8mb4','user', 'pass');
$sql = 'SELECT id, name FROM 1/1/16';
$results = $db->query($sql);
if ($results->num_rows > 0) {
while($row = $results->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["name"];
}
}
?>
How can I get rid of this error? Thank you :)
Your table name seems to be unusual. Try to use back-ticks to quote it:
$sql = 'SELECT id, name FROM `1/1/16`';
This question already has answers here:
Single result from database using mysqli
(6 answers)
Closed 2 years ago.
I am trying to pull a single value from a database and assign it to a php variable. All of the mysqli functions appear to pull an entire row, while I want one value of that row (ex. ID, name, ect).
This is what I have so far:
$result = mysqli_query($con, "SELECT * FROM test_table WHRE ID='" . $_GET['ID'] . "'");
$row = $result->fetch_assoc();
$test= $row['ID'];
echo $test;
When I run the above I don't get any output; $test is unassigned. What is the correct command to assign a value to my $test variable?
You forgot a E in youy WHERE clause
$result = mysqli_query($con, "SELECT * FROM test_table WHERE ID='" . $_GET['ID'] . "'");
$row = $result->fetch_assoc();
$test= $row['ID'];
echo $test;
If your 'ID' field is a integer, quotes are not necessary.