sql data fetch bug [duplicate] - php

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);

Related

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

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.

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 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

php warning why ? unable to solve it [duplicate]

This question already has answers here:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result [duplicate]
(6 answers)
Closed 8 years ago.
I am getting a warning message which I don't understand why and unable to resolve, (see below)
Warning: Supplied argument is not a valid MySQL result resource in /detail.php on line 34
Here is my code:
$rs = mysql_query($strSQL);
$strSQL = "SELECT * FROM <tablename> WHERE id=" . $_GET["serviceName"];
// Loop the recordset $rs
while($row = mysql_fetch_array($rs)) **(line 34) here ***
{
echo $row['ID']."<br />";
echo $row['serviceName']."<br />";
// Close the database connection
mysql_close();
?>
</dl>
<p>Return to the list</p>
</body>
</html>
thanks in advance, I am not getting any of the data on this webpage either,thanks...singhy
The query is failing - you need to wrap quotes around strings in MySQL:
$strSQL = "SELECT * FROM gu_service_cat WHERE id = '" .
$_GET["serviceName"] . "'";
plus, the $rs should be BELOW the $strSQL...
You need to do it like this
$strSQL = "SELECT * FROM gu_service_cat WHERE id=" . $_GET["serviceName"];
$rs = mysql_query($strSQL);
Because before setting value to the variable you are using it in the query. This is why it is throwing the error.

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