This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 6 years ago.
I'm a beginner with PHP and I'm stucked with a simple request that returns nothing.
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM order";
$result = $conn->query($sql);
// output data of each row
while($row = $result->fetch_assoc()) {
echo "something";
}
$conn->close();
This table is not empty.
I'm clueless...
Your code looks alright.
To get errors that may have occured you can change the following:
$result = $conn->query($sql);
if(!$result) {
die($conn->error);
}
echo "Result rows: "+ $result->num_rows;
You might find an error this way.
Related
This question already has answers here:
How to get count of rows in MySQL table using PHP?
(3 answers)
Closed 2 years ago.
I'm just putting a simple PHP program together that simply counts the number of total rows in my database and returns the result as a JSON object when it recieves a get request from my frontend.
This should all be super simple but for the life of me I can't figure out how to retrieve the count result in a usable format. All I get from this is "null"
any ideas?
$con = mysqli_connect($servername, $username, $password, $dbname);
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
$query="SELECT COUNT(*) FROM database";
$result = mysqli_query($con,$query);
$count = mysqli_fetch_assoc($result);
echo json_encode($count);
You can use COUNT as to avoid a very long key.
Here's a code example
$sql = "SELECT COUNT(column) as count FROM mytable";
$stmt = mysqli_stmt_init($db);
mysqli_stmt_prepare($stmt, $sql);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$count = mysqli_fetch_assoc($result)['count'];
echo $count;
To display data in JSON you can try this:
echo json_encode(['count' => $count])
That by itself will not give you the count.
I would suggest that you start using MySQL object methods instead of functions.
You should also use the "as" satement as way to give a name to the column that has the count value; in my example that column will be known as C .
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$query = "SELECT COUNT(*) as C FROM database";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
$row = $result->fetch_assoc();
echo $row["C"];
}
EDIT:
In the specific case of SELECT COUNT you'll only get 1 row, so for this specific example you could do just fetch_assoc().
This question already has answers here:
How to make mysqli connect function?
(2 answers)
Closed 6 years ago.
I have to select data from MySQL Database. I have been looking for the answer, but still haven't found any. I am learning from W3School
My MySQL doesn't have any username or password, so my code looks like this:
<?php
$servername = "localhost";
$dbName = "db_Test";
//Create Connection
$conn = mysqli_connect($servername, $dbName);
//Check Connection
if(!$conn){
die("Connection Failed. ". mysqli_connect_error());
}
$sql = "SELECT ID, Name, Category, Description, Price FROM items";
$result = mysqli_query($conn, $sql);
if(!$result){
die(mysqli_error($conn));
}
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
echo "ID: ".$row['ID']." Name: ".$row['Name']." Category: ".$row['Category']."<br>";
}
}
?>
First I got this error
mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in
On further tracing it back in the stack, I got this:
No database selected
I don't know what I'm doing wrong. So can you explain it to me and give me the solution. Thanks in advance
Set mysql username and passwork something like this.
$username = "root";
$password = "";
And set connection like this.
$conn = mysqli_connect($servername, $username, $password, $dbname);
This question already has answers here:
Object of class mysqli_result could not be converted to string
(5 answers)
Closed 6 years ago.
Im having problems while trying to fetch an id of a specific string in mySQL
My table:
ID | URL |
1 | http://google.com |
When I put this V
SELECT ID FROM urls
WHERE url LIKE ('http://google.com')
code into mySQL it does show the ID 1, but Im trying to let it display on a page on my website.
The code below is the code of the page where Im trying to display this code.
<?php
// Create connection
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// sql to create table
$url = 'http://google.com';
$ip = $_SERVER['REMOTE_ADDR'];
$sql = "
SELECT ID FROM urls
WHERE url LIKE ('http://google.com')
";
$result = $conn->query($sql);
echo $result;
$conn->close();
Yes I have defined DB_HOST, DB_USER, DB_PASS and DB_NAME and they work fine.
Whenever I execute the code, this error message appears.
Catchable fatal error: Object of class mysqli_result could not be converted to string in /home/u926065153/public_html/db_t.php on line 16
Please note I dont have a lot of experience with SQL and this is my way of learning.
you should fetch the result.
http://php.net/manual/en/mysqli-result.fetch-array.php
That is,
$row = $result->fetch_array(MYSQLI_NUM);
var_dump($row);
You could see all the rows as array.
Your code will be like this.
<?php
// Create connection
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// sql to create table
$url = 'http://google.com';
$ip = $_SERVER['REMOTE_ADDR'];
$sql = "
SELECT ID FROM urls
WHERE url LIKE ('http://google.com')
";
$result = $conn->query($sql);
$row = $result->fetch_array(MYSQLI_NUM);
echo $row[0] . " " . $row[1];
$conn->close();
This question already has answers here:
How do I escape reserved words used as column names? MySQL/Create Table
(4 answers)
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 6 years ago.
I have the following code
<?php
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "database";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, name, time, order FROM main";
$result = mysqli_query($conn, $sql);
$num = mysqli_num_rows($result);
echo "Stuff: " . $num;
mysqli_close($conn);
?>
For some reason, this does not return a value when uploaded to my server and run on my web browser. Can anyone understand why this might be?
Many thanks
I'm new to MySQL and PHP and I m struggling to echo my queries (the results not the text!)
I have searched for this but nothing seems to work properly, the best I managed to do was echoing the text of the query. I might have some fatal mistakes but here is my code:
<?php
$username = "root";
$password = "";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
$selected = mysql_select_db("atoma",$dbhandle)
or die("Could not select atoma");
$sql1 = mysql_query("SELECT (MAX(GRADE)) as maximum FROM thema3");
$r = mysql_fetch_array($sql1); // will return the result
echo $r['maximum'];
$sql2 = "SELECT COUNT(DISTINCT NAME) FROM thema2";
$sql1 = "SELECT AVG(GRADE) FROM thema3";
mysql_close($dbhandle);
?>
I get nothing as a result.
I have these 3 queries and all I want is just to print their results. I've written code for echoing only one of the 3 since the other 2 will be echoed as the first one I want to believe.
Your code seems incorrect because, the connection is mysqli and fetching is using mysql
$conn = new mysqli($servername, $username, $password, $dbname);
....
$sql1 = mysql_query("SELECT (MAX(GRADE)) as maximum FROM thema3");
$r = mysql_fetch_array($sql1); // will return the result
A full example of W3Schools
<?php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
http://www.w3schools.com/php/php_mysql_select.asp
When you use max, avg, etc you pull only one result, so with the $result[0] you has the result you want
Edit:
If you're new, maybe you come to see read this:
http://codular.com/php-mysqli
So A) would leave using an outdated way to call the database, and B) with this in principle bringing the first row you would have the result of AVG, MAX, etc. when only one row which returns you if you make this types of sentences ;)