Whats wrong with this code? [MySQL request cell info] [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
What's wrong with this?
$result = mysql_query("SELECT * FROM users WHERE username='$username'");
$row = mysql_fetch_row("$result");
$id = $row[2];

$row = mysql_fetch_row($result);
when you use a variable inside a double quoted string it will be casted to a string. and you cannot pass mysql_fetch_result a string, but only a mysql result

The following statement:
$row = mysql_fetch_row("$result");
must be like this:
$row = mysql_fetch_row($result);

please correct this line
$row = mysql_fetch_row("$result");
to
$row = mysql_fetch_row($result);
and you should be good to go

U dont need the "s for the variable in 2nd line...try debugging using var_dump in each step and see where the error is coming in these kinds of situations. U can also see if the query had any problems by doing this
$result = mysql_query("SELECT * FROM users WHERE username='$username' ") or die("Error with query");

Try this
$result = mysql_query("SELECT * FROM users WHERE username='".$username."'");
$row = mysql_fetch_row($result);

Related

What is wrong with this php mysql code? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
require("./connect.php");
$getid = $_GET['id'];
$getusername = mysql_query("SELECT username FROM user WHERE id='$getid'");
$getdesc = mysql_query("SELECT description FROM user WHERE id='$getid'");
echo "$getusername $getdesc";
I am having trouble, it is not echoing the data from those variables. I is returning resource id #10 and #11.
You need to fetch the data first before you can use the mysql_query result...
please see the example in the PHP Documentation
https://php.net/manual/en/function.mysql-fetch-row.php
<?php
$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
echo $row[0]; // 42
echo $row[1]; // the email value
?>
Warning:
This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.
pdo : https://php.net/manual/en/book.pdo.php
mysqli : http://www.php.net//manual/en/book.mysqli.php
You are trying to print out the resource ID of the query you just ran.
To get to the actual results you have to specifically request it.
mysql_fetch_assoc($getusername); //should be used!

what is error in following code [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
$result = mysqli_query($connection,"SELECT * FROM libsutdent where libid='$_POST[libid]'");
$rowcount=mysqli_num_rows($result);
if($rowcount==1)
{
while($row = mysqli_fetch_array($result))
{
$libid=$row['libid'];
$regno= $row['regno'] ;
$name= $row['stuname'] ;
$branch= $row['branch'] ;
$semester= $row['semester'] ;
$section= $row['section'] ;
$yearofadm= $row['yearofadm'];
}
}
Dont post anything directly in database as its a threat to data security (SQL Injection)
$libid = $_POST['libid'];
$libid = mysqli_real_escape_string($connection, $libid);
$result = mysqli_query($connection,"SELECT * FROM libsutdent where libid='".$libid."'");
Make sure that your mysql field is really libsutdent. Seems like it should be libstudent.
Then Place {} around your Post variable. ie {$_POST[\"libid\"]}.
Conversely you can place another step in your code like:
$libid = $_POST["libid"];
I think you can do without the quotes around libid, but I always think it reads better to add them.
$result = mysqli_query($connection,"SELECT * FROM libsutdent where libid='$_POST[libid]'");
Should be
$result = mysqli_query($connection,"SELECT * FROM libsutdent where libid='".mysql_real_escape_string($_POST['libid'])."'");

PHP: How to check if no rows are returned [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
With the following PHP code, how do i check if there's no row retuned so i can echo some message?
PHP:
<?php
require_once 'db_conx.php';
$Result = mysql_query("SELECT * FROM ads WHERE pid = '2'")
or die (mysql_error());
while($row = mysql_fetch_array($Result)){
echo '<span class="classPid" style="display:none">'.$row['pid'].'</span>';
}
?>
Thanks
There are a number of ways to do this. There is a specific function mysql_num_rows() which will return the number of rows returned.
mysql_num_rows($Result);
This will return 0 if there are no rows affected or returned.
You could also create some conditions using mysql_fetch_array. mysql_fetch_array will return FALSE if there are no rows.
On a separate note it would be a good idea to update your connection
and functions to mysqli as mysql is no depreciated.
See docs on mysql_num_rows().
http://www.php.net/mysql_num_rows
Something like this
<?php
require_once 'db_conx.php';
$Result = mysql_query("SELECT * FROM ads WHERE pid = '2'")
or die (mysql_error());
$i=0;
while($row = mysql_fetch_array($Result)){
echo "<span class='classPid' style='display:none'>".$row['pid']."</span>";
$i++;
}
if($i==0){
echo "No rows found";
}
?>

Can this code work or not [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can the code below work or not to retrieve data from database and display.. I know that the sql4 can't work in a mysql database can the php code below make it work or not.
<?php
include 'connect.php';
$sql2 = "SELECT * from pm Order by mid";
while ($row3 = mysql_fetch_array($sql2)) {
$sql4 = mysql_query("SELECT * FROM reply ORDER BY mid =".$row3["$mid"]." ");
$row4 = mysql_fetch_array($sql4);
echo"<trid='".$mid."'><td><img src='a/$name' width='150' height='100' /></td> <td>".$row['mid']."</td></tr><tr><td>".$row['reply']."</td></tr>";
}
?>
esp. this mysql or mysql and php combined. Basically is the code below improper. I think it is.
<?php
$sql4 = mysql_query("SELECT * FROM reply ORDER BY mid =".$row3["$mid"]." ");
?>
Put the following at the top of your PHP file and try it out yourself:
<?php
error_reporting(-1);
ini_set("display_errors", true);
// Your code goes here
?>
(I think the code is self-explaining)
Try...
$sql2 = "SELECT * from pm Order by mid";
$result = mysql_query($sql2); <- MISSING THIS LINE
while ($row3 = mysql_fetch_array($result))
{
// OTHER CODE HERE
}

Convert Json Array to Strings [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have my result in json array format, I want to change my array results to json string format. How do I do this. Please help.. i dont have idea how i go about doing this :(. new to json
My result as json array is like this
[["Date.UTC(2011,09,03)","1"],["Date.UTC(2011,09,06)","53"],["Date.UTC(2011,09,07)","178"],["Date.UTC(2011,09,08)","305"],["Date.UTC(2011,09,09)","152"],["Date.UTC(2011,09,11)","20"],["Date.UTC(2011,09,12)","239"],["Date.UTC(2011,09,13)","25"],["Date.UTC(2011,09,14)","316"],["Date.UTC(2011,09,15)","169"],["Date.UTC(2011,09,16)","20"],["Date.UTC(2011,09,19)","126"]
My code
$mysql_connect = mysql_connect($db_host, $db_user, $db_pword, $db_name);
$query = "Select DATE_FORMAT(`timestamp`,'%Y,%m,%d') as date, Count(*) as frequency from mytable group by date_format(`timestamp`,'%Y,%m,%d')";
if (!$mysql_connect) die("unable to connect to database: " . mysql_error());
#mysql_select_db($db_name) or die( "Unable to select database");
$result = mysql_query($query);
$response = array();
if($result === FALSE)
{
die(mysql_error()); // TODO: better error handling
}
while($row=mysql_fetch_array($result))
{
$date = "Date.UTC(".$row ['date'].")";
$frequency =$row['frequency'];
$response[] = array ($date,$frequency);
}
JSONArray jArray;
String s = jArray.toString();
mysql_close($mysql_connect);
json_encode:
<?php
$json_string = json_encode($your_array);
Did you even bother to Google this?

Categories