This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 5 years ago.
I think, I might need help with my PHP code...
I'm trying to echo the info in a MySQL database and it gives me the Error: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /usr/local/ampps/www/php/db.php on line 18
My code is:
<?php
$servername = "blabla"; //changed, connecting works
$username = "blabla";
$password = "blabla";
$database = "blabla";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo "Connection success \n";
}
$sql = mysql_query("SELECT * FROM Schueler");
while($data = mysql_fetch_array($sql)) //This is line 18...
{
echo "ID: " . $data['ID'] . " Vorname: " . $data['Vorname'] . " Nachname: " . $data['Nachname'] . " Klasse: " . $data['Klasse'] . "<br>";
}
$conn->close();
?>
Would be nice if somebody could help me :)
Edit:
I fixed it using MySqli only, this is my working code now:
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo "Connection success \n";
}
$sql = "SELECT ID, Vorname, Nachname FROM Schueler";
$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["Vorname"]. " " . $row["Nachname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
Thanks for the quick advice.
You have used mysqli for your connection setup, then you used simple mysql functions to get the resultset and the fetch_array logic. You need to be uniform in this case.
I have changed the mysql_query() call to mysqli_query() call, and similarly the mysql_fetch_array() call to mysqli_fetch_array() call.
The final code becomes:
$sql = mysqli_query("SELECT * FROM Schueler", $conn);
while($data = mysqli_fetch_array($sql))
{
echo "ID: " . $data['ID'] . " Vorname: " . $data['Vorname'] . " Nachname: " . $data['Nachname'] . " Klasse: " . $data['Klasse'] . "<br>";
}
Related
I have a page that shows some of the information in the database. I'd like to add a link in each row (like making the first name a link) that I can click on that brings me to a page that shows the rest of that row's info (like a profile page). I'm thinking of making a link that passes the id to the profile page so that the profile page can gather the info.
I'm sure this is simple, but I'm just not getting it. How do I make the link show up in each row that sends only that row's id number? Because I'd rather not have to go into each row and make a special link.
Here is the code I have:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// 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, Phone, Email FROM Contacts";
$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"]. "
" . $row["Phone"]. "
" . $row["Email"]. " <br>";
}
} else {
echo "0 results";
}
$conn->close();
?>```
Based on your example you could do this:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// 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, Phone, Email FROM Contacts";
$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"] . " " . $row["Phone"] . " " . $row["Email"] . " <br/>";
}
} else {
echo "0 results";
}
$conn->close();
?>
That profile.php page would check if the id is set with isset and query the data based on id simular to this: PHP & MYSQL: Select from where id=$id
Not tested and make sure to sanitize any user-generated variables.
EDIT - Thanks so much to everyone's help. In continuing to learn, I am now working a different statement based on what I learned above but it is giving no result whatsoever. Workbench shows the query statement as good and displays result as "2"
<?php
$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT SUM(`mattiscool`),booking_date FROM `wp_cbxrbooking_log_manager` WHERE `booking_date` = CURDATE() -1";
$result = mysqli_query($sql);
while ($row = mysqli_fetch_assoc($result))
{
echo $row['total'];
}
mysqli_close($con);
?>
I am trying to call via php an MySQL Statement and have that output the values. Below is what I have tried. Could someone please help enlighten me as to what I am doing wrong?
<?php
$sql = "SELECT DAYNAME(wp_cbxrbooking_log_manager.`booking_date`) as \'weekday\', \n"
. " wp_cbxrbooking_log_manager.`party_size` as \'Party Size\',\n"
. " wp_cbxrbooking_log_manager.`booking_time` as \'Time\',\n"
. " wp_cbxrbooking_log_manager.`mattiscool` as \'# of Reservations\'\n"
. "FROM wp_cbxrbooking_log_manager\n"
. "\n"
. "WHERE `booking_date` >= DATE(NOW()) - INTERVAL 7 DAY \n"
. "ORDER BY `Time` ASC";
?>
Your code is incomplete, you have to establish a connection to database and then execute the query to get the output. You can find some good examples here and here
You need to execute your query and fetch the result as follows:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT DAYNAME(wp_cbxrbooking_log_manager.`booking_date`) as \'weekday\', \n"
. " wp_cbxrbooking_log_manager.`party_size` as \'Party Size\',\n"
. " wp_cbxrbooking_log_manager.`booking_time` as \'Time\',\n"
. " wp_cbxrbooking_log_manager.`mattiscool` as \'# of Reservations\'\n"
. "FROM wp_cbxrbooking_log_manager\n"
. "\n"
. "WHERE `booking_date` >= DATE(NOW()) - INTERVAL 7 DAY \n"
. "ORDER BY `Time` ASC";
$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();
?>
I'm a bit new to MySQL and I am trying to create a search system. I am using some simple PHP code from w3schools.
I connect to my database:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "img";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
Then echo the results.
$sql = "SELECT ID, Name, Description, Path FROM system";
$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["Name"]. " " . $row["Description"]. " " . $row["Path"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
My database contains one entry, but when I run this code, "0 Results" is echoed to the screen. Is there something I am doing wrong here?
I'm trying to read from my database and display the info in my browser but it shows up a blank page,how do i fix this?
This is my html form:
This is my php script:
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['submit'])){
$sql = "SELECT * FROM mydb";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "name: " . $row["name"]. " - email: " . $row["email"]. " " . $row["phone"]. "<br>";
}
} else {
echo "0 results";
}
}
$conn->close();
?>
Thank you in advance
In my PHP code, i'm easily writing records to my database but for some reason i can't read anythign out. My PHP code is:
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM companies";
if ($conn->query($sql) === TRUE)
{
echo "query success";
while($row = $result->fetch_assoc())
{
echo "ID: " . $row["ID"]. " - Name: " . $row["name"]. "<br>";
}
}
else
{
echo "query failure";
echo "Error: " . $sql . "<br>" . $conn->error;
}
$sql = "INSERT INTO companies (name)
VALUES ('mycompany')";
if ($conn->query($sql) === TRUE)
{
echo "insert success";
}
else
{
echo "insert failure";
echo "Error: " . $sql . "<br>" . $conn->error;
}
The output I get from the browser when i run it is:
query failureError: SELECT * FROM companies
insert success
I've tried variations of apostrophes, carets, quotes in that $sql string. I've tried running this query in HeidiSQL and it works fine. Any ideas where I'm going wrong? Any suggestions of more basic stuff I can try to narrow down the source of the problem?
thanks!
Using mysqli->query() with a SELECT statement returns an instance of mysqli_result. It is not identical to true (=== true), but nor does it represent an error.
Moreover, $result is undefined.
Use this instead:
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM companies";
if (($result = $conn->query($sql)) !== FALSE)
{
echo "query success";
while($row = $result->fetch_assoc())
{
echo "ID: " . $row["ID"]. " - Name: " . $row["name"]. "<br>";
}
}
else
{
echo "query failure";
echo "Error: " . $sql . "<br>" . $conn->error;
}
...
This simply changes your === TRUE check to !== FALSE. MySQLi::query() returns boolean FALSE on failure, boolean TRUE on a successful query without result sets or a mysqli_result upon success with a result set.
This also assigns the result of query() into $result.
You have not assign the query result to $result variable.
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM companies";
$result = $conn->query($sql);
if ($result === TRUE)
{
echo "query success";
while($row = $result->fetch_assoc())
{
echo "ID: " . $row["ID"]. " - Name: " . $row["name"]. "<br>";
}
}
else
{
echo "query failure";
echo "Error: " . $sql . "<br>" . $conn->error;
}
$sql = "INSERT INTO companies (name)
VALUES ('mycompany')";
if ($conn->query($sql) === TRUE)
{
echo "insert success";
}
else
{
echo "insert failure";
echo "Error: " . $sql . "<br>" . $conn->error;
}
The problem is that a SELECT query will return a mysqli_result object on SUCCESS not a boolean TRUE. Only if the query fails, will it return a boolean FALSE;
Therefore you should use it like this :
$result = $conn->query($sql);
if ($result !== FALSE){
while($row = $result->fetch_assoc())
{
echo "ID: " . $row["ID"]. " - Name: " . $row["name"]. "<br>";
}
}
just change your if condition :-
if ($result = $conn->query($sql))
{
echo "query success";
while($row = $result->fetch_assoc())
{
echo "ID: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
}
}