Call to a member function query() on resource [duplicate] - php

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 4 years ago.
okay guys so i was just writing the code for an admin panel just for fun, and i ran into a problem when i copied some code from w3schools.
Can someone tell me where i am wrong.
Here's My Code
<?php
$conn = mysql_connect("localhost", "root","","ATFlogin");
$sql = "SELECT id, username, password FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<br> id: ". $row["id"]. " - Name: ". $row["firstname"]. " " . $row["lastname"] . "<br>";
}
} else {
echo "0 results";
}
?>

it Looks like you are trying to mix mysql with mysqli.
Change your connection to be:
$conn = mysqli_connect("localhost", "root","","ATFlogin");
Read up on MySQLI here
mysql functions have all been depracted and mysqli is the successor so if possible use mysqli.

Related

Trying to get data from 2 tables from mysql to show up in php [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 5 years ago.
I need assistance in understanding how to work with 2 tables from a database. I tried writing the code like this but it did not work. It works in MySQL but not in php. I am new coder trying to learn, but I am stuck any help would be appreciated. first and last name are on 1 table. Price is on another table.
Also I am getting this error
mysql_fetch_array() expects parameter 1 to be resource
index.php
<?php
include ('db.php');
$sql='SELECT * FROM `user_info` ,`customer_order` WHERE user_info.user_id=customer_order.uid';
$run_query=mysqli_query($conn,$sql);
if(! $run_query ) {
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($run_query, MYSQL_ASSOC)) {
echo "First Name:{$row['first_name']} <br> ".
"Last Name:{$row['last_name']} <br>".
"price:{$row['price]} <br>";
}
I made a mistake and mixed up MySQL with mysqli I fixed the issue and it is working thanks from Don't panic.
<?php
include ('dbconnect.php');
$sql='SELECT * FROM `user_info` ,`customer_order` WHERE user_info.user_id=customer_order.uid';
$run_query=mysqli_query($conn,$sql);
if(! $run_query ) {
die('Could not get data: ' . mysqli_error());
}
while($row = mysqli_fetch_array($run_query, MYSQLI_ASSOC)) {
echo "name:{$row['first_name']} <br> ".
"Last:{$row['last_name']} <br> ";
}

mysql_fetch_array() recieving null value [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 5 years ago.
Cant figure out why my query isn't returning the 'make'. I've checked the SQL many times.
$DBConnect = #mysqli_connect("host.com", "name","pass", "database")
Or die ("<p>Unable to connect to the database server.</p>". "<p>Error code ". mysqli_connect_errno().": ". mysqli_connect_error()). "</p>";
$query = "SELECT make FROM inventory";
$makeresult= #mysql_query($DBConnect, $query);
while($row = mysql_fetch_array($makeresult)) {
$options .="<option>" . $row['make'] . "</option>";
}
You're using a mysqli connection but trying the deprecated mysql functions.
try mysqli_fetch_array() instead

sql data fetch bug [duplicate]

This question already has answers here:
Why shouldn't I use mysql_* functions in PHP?
(14 answers)
Closed 6 years ago.
I have two pages . In the first page if user will click on button then he will get relavant data with id column
<form action='edit.php?id=$id' method='post' name='edit_btn'>
<button type='submit' class='w3-btn w3-red w3-round-xlarge'>Proceed </button>
</form>
$con=mysqli_connect("localhost","root","","student_result_info_db");if (mysqli_connect_errno()){ echo "Failed to connect to MySQL: " . mysqli_connect_error(); }else{
if(isset($_GET['edit_btn'])){
echo "<div class='w3-container w3-red'> <h1>>Error Found!</h1> <h4>OK You Did Hit The EDIT Button</h4> </div> <br/>";
}else{
$get_selected_id = $_GET['id'];
echo $get_selected_id;
$res = "SELECT * FROM school_result";
while ($row = mysql_fetch_array($res)){
echo "$row[id]. $row[first_name] <br/>";
echo "$row[id]. $row[last_name] <br/>";
}
}
}
It'showing following error
Fatal error: Call to undefined function mysql_fetch_array() in
Use mysqli_fetch_array instead, along with mysqli_query to get a mysqli_result object:
$qresult = mysqli_query($con, $res);
while ($row = mysqli_fetch_array($qresult)){
You need to use this for 2 reasons:
You opened your database using mysqli_connect, so you need to use the mysqli functions.
mysql functions were removed in PHP 7.
Use mysqli function instead of mysql like
mysqli_query($con, $res);

Sql echo row with php [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 7 years ago.
I can connect to DB but i cant echo , all time that said 0 results but my DB have TABLE (text) with 2 COLUMN id and text and i have inserted text in my table.
$con = mysqli_connect("localhost","1078362","nenaddurmisi022");
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT vesti FROM comment";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0)
{
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo $row["vesti"]."<br>";
}
}
else {
echo "0 results";
}
mysqli_close($con);
You're using all mysqli functions except for your database connection. Try changing the first line to...
$con = mysqli_connect("localhost", "1078362", "nenaddurmisi022");
...and change the last line to...
mysqli_close($con);
I'm guessing there are a lot of errors generated by this script, but you're not seeing them. You should add these lines to the top of the script...
error_reporting(E_ALL);
ini_set('display_errors', 1);
try
$sql = "SELECT * FROM text";
instead of
$sql = "SELECT text FROM text";
also be sure you'r table name is text.
one last thing you could try is to replace
echo $row["text"]."<br>";
by
print_r($row);
to see if there's a result set from you'r Database

Why can not I fetch data from mysql db table with mysqli_query? [duplicate]

This question already has answers here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(2 answers)
Closed 6 years ago.
I am at prototype stage so I have error_reporting(-1); at my 1st row. Despite this, I have no php error but php prints 'could not get data'.
As I understood from php.net manual and stackoverflow similar cases, my $sorgula returns FALSE. But why? Can you help, regards
//i am sure that i am connected to db
if ($sorgula = mysqli_query($dbc, "SELECT * FROM tb_yazilar ORDER BY kolon_sn"))
{
while ($satir = mysqli_fetch_array($sorgula, MYSQLI_ASSOC))
{
echo $satir['kolon_yazar'].' - '.$satir['kolon_baslik'].' - '.$satir['kolon_yazi'].' - '.$satir['kolon_etiketler'].' - '.$satir['kolon_ytarihi'].' - - - - ';
}
}
else
{
echo 'could not get data';
}
mysqli_close($dbc);
try to use mysqli_error in your code .
procedural example:
$sorgula = mysqli_query($dbc, "SELECT * FROM tb_yazilar ORDER BY kolon_sn")
or error_log(mysqli_error($dbc));
I used this and it worked: without the if, once it extracts, go back and add the if. :)
require 'db.php';
$query = "SELECT * FROM thoughts";
$result = mysqli_query($conn, $query);
while($row=mysqli_fetch_assoc($result)) {
echo "<td>" . "TEXT: ". $row['text'] . "</td>";
}
mysqli_close($conn);

Categories