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);
Related
Thank you in advance!
Please tell me! what is mistake in this code. I want to show total rows of sql on my site. I searched many of codes be like this but all failed. Please help me.
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password, 'sahiwalservices');
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "hunnysheikh99";
;
mysqli_select_db("sahiwalservices", $conn);
$result = mysqli_query("select count(1) FROM login");
$row = mysqli_fetch_array($result);
$total = $row[0];
echo "Total rows: " . $total;
mysqli_close($conn);
Posting as a community wiki; I don't want rep from this.
mysqli_select_db() - The syntax for that is:
Db connection comes first
Database name is second
Yet, you don't need that line since you're already passing that in:
$conn = new mysqli($servername, $username, $password, 'sahiwalservices');
Then, you didn't pass db connection to the query.
Example from the manual:
if ($result = mysqli_query($link, "SELECT Name FROM City LIMIT 10"))
Reference:
http://php.net/manual/en/mysqli.query.php
Check for errors on the query also:
http://php.net/manual/en/mysqli.error.php
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
So here is my code:
$sql = "SELECT * FROM `items`";
$result = mysqli_query($conn, $sql);
while($prommes = mysql_fetch_array($result)){
}
When I'm running it. I'm getting this error:
mysql_fetch_array() expects parameter 1 to be resource.
Is it something wrong with my database?
And here is my connection which Irequire in the html document. Can it be something here which makes the query not working?
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// echo "Connected successfully";
You never select a database, as far as I can tell, so $result is not a resultset (because the query never executes properly). It is probably false instead. You can var_dump it to verify that. Try adding the database name in the query. If your database is named db, for example:
SELECT * FROM `db`.`items`;
If you don't want to explicitly specify the database name every time, you'll just want to add the database name to the MySQLi connection string, as shown:
http://php.net/manual/en/mysqli.query.php
In the while loop, mysql_fetch_array is incorrect. It should be
while($prommes = mysqli_fetch_array($result))
Additonally your connection needs to be
$conn = mysqli_connect($servername, $username, $password, $database);
where $database is the name of your database (not the hostname).
As an aside, this can be better avoided using an object orientated approach. So for example, your connection could be;
$conn = new mysqli($servername, $username, $password, $database);
and your query and loop could be;
$result = $conn->query($sql);
while($prommes = $result->fetch_array())
Hope this helps.
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.
This question already has answers here:
Selecting random rows with MySQL
(3 answers)
Closed 8 years ago.
bellow you can see my table. In this table we have 300 quotes that I'm trying to fetch my at random and display it on the page but I have not succeeded. The column containing the texts has the name "fortune_text". here is my attempt code:
<?php
$username = "fortunes";
$password = "xxxx";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
// connect to database
mysql_select_db("fortunes");
// query the databse
$query = mysql_query("SELECT 'fortune_text' FROM 'fortunes' ORDER BY RAND()");
echo "$query";
?>
try using shuffle() function of php to randomize array that comes from database and then echo the first index of that variable.
Hi here is the modified version of your sample, which fetch text by rand order.
I suggest you to use limit when you print result to page if you have later many rows, checking for error if any. Important line is echo mysql_result($result);
<?php
$username = "fortunes";
$password = "xxxx";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
// connect to database
mysql_select_db("fortunes");
// query the databse
$result = mysql_query("SELECT 'fortune_text' FROM 'fortunes' ORDER BY RAND()");
if (!$result) {
die('Could not query:' . mysql_error());
}
echo mysql_result($result);
mysql_close($dbhandle);
?>