I am pretty sure this is a syntax problem, but there may be other issues as well.
Ultimately, I'm trying to create a variable from an array that will be used in an insert statement, but I can't get past a select statement with a variable.
For a while I had $country_id = $coun; as $country_id = mysqli_real_escape_string($coun); but the var_dump suggested that the mysql_real_escape_string was preventing $country_id from taking on the value of $coun.
When I check for errors on $q, the query it spits out works just fine on phpmyadmin, yet the array outputs a NULL value.
I'm stumped.
Here is the code:
//get short_name variable
$country_id = $coun;
//var_dump($coun, $country_id);
$q = "SELECT short_name FROM country WHERE country_id = $country_id LIMIT 1";
$r = mysqli_query($dbc, $q);
$num = mysqli_num_rows($r);
if ($num > 0) {//match was made
//Get short_name
$row = mysqli_fetch_assoc($r, MYSQLI_ASSOC);
//var_dump($row);
}else {
echo '<p>no match</p>';
}
I just got some help and it turns out that the problem was that "assoc" should have been "array." it looks like this now. $row = mysqli_fetch_array($r);
Related
$sql = "SELECT temail FROM teacherusers WHERE tfullname='$teachername' limit 1";
$result = mysql_query($sql);
$value = mysql_fetch_object($result);
$teacheremail2 = $value->temail;
echo $teacheremail2;
echo $teacheremail2 returns nothing.
$teachername is valid and i have checked multiple times.
It should be a two-dimensional array , you need
$value[0]->temail
The result of mysql_fetch_object($result) is an object(stdClass).
The explanation of object(stdClass) ican be found at this link
$sql = "SELECT temail FROM teacherusers WHERE tfullname='$teachername' limit 1";
$result = mysql_query($sql);
while ($value = mysql_fetch_object($result))
{
$teacheremail2 = $value->temail;
echo $teacheremail2;
}
First off, you'll want to run the query directly against your database to ensure that the query returns some kind of result.
Secondly, if that works, you'll want to echo $value directly to check that you are getting results back on the webpage.
Then you can check if temail is a field of $value
$sql = "SELECT temail FROM teacherusers WHERE tfullname='$teachername' limit 1";
$result = mysql_query($sql);
while ($value = mysql_fetch_object($result))
{
$teacheremail2 = $value->temail;
echo $teacheremail2;
}
hope this help
$sql= "SELECT team_name AS label, user_id as value
FROM d_getreal_captains ORDER BY team_name ASC";
$result = $bfit_connect->query($sql);
$rows = mysqli_fetch_assoc($result);
while( $rows = mysqli_fetch_assoc( $result ) ) {
$json[] = $rows;
}
This code works as expected except it does not return the first record from the table (i.e. the first team_name and user_id). I have seen that other people have suggested the fix of using mysql_data_seek to reset the row index, but I also know that is deprecated and would like my fix to be utilizing a current method. And ideas or advice is appreciated... thanks.
You're calling mysqli_fetch_assoc() outside of your loop which is popping the first result off of the result set. You aren't using the values anywhere so you can safely remove it.
$result = $bfit_connect->query($sql);
$rows = mysqli_fetch_assoc($result); // <-- remove this
$sql= "SELECT team_name AS label, user_id as value
FROM d_getreal_captains ORDER BY team_name ASC";
$result = $bfit_connect->query($sql);
//Here you used to take the first row out with mysqli_fetch_assoc, I removed this line
while( $rows = mysqli_fetch_assoc( $result ) ) {
$json[] = $rows;
}
I am using PHP/MySql to display some results from a database.
CODE
$query = "select * from users where (fname like '%".$searchTerm."%') OR (lname like '%".$searchTerm."%')";
$result = $db->query($query);
echo "yoooo";
$num_rows = $result->num_rows;
echo "<br/>".$num_rows." results st 2";
for ($i=0; $i<$num_rows ; $i++) {
$row = $result->fetch_assoc();
$fn = $row['fname'];
$ln=$row['lname'];
echo "<br/>".stripslahes($fn)." ".stripslashes($ln);
}
This shows:
yoooo
1 results st 2
But nothing more... Why? I am sure that the names I use in the associative array are the column names in the table...
I'd try to:
turn on error_reporting(E_ALL)
echo the query + make sure $searchTerm is safe(avoid sql injections)
var_dump($row)
Is it possible to re-write the code below, maybe even with an if (result > 0) statement, in just one line (or simply shorter)?
// a simple query that ALWAYS gets ONE table row as result
$query = $this->db->query("SELECT id FROM mytable WHERE this = that;");
$result = $query->fetch_object();
$id = $result->id;
I've seen awesome, extremely reduced constructs like Ternary Operators (here and here - btw see the comments for even more reduced lines) putting 4-5 lines in one, so maybe there's something for single result SQL queries like the above.
You could shorten
$query = $this->db->query("SELECT id FROM mytable WHERE this = that;");
$result = $query->fetch_object();
$id = $result->id;
to
$id = $this->db->query("SELECT id FROM mytable WHERE this = that")->fetch_object()->id;
but this, and the original code will emit errors, if any of the functions returns an unexpected response. Better to write:
$query = $this->db->query("SELECT id FROM mytable WHERE this = that");
if (!$query) {
error_log('query() failed');
return false;
}
$result = $query->fetch_object();
if (!$result) {
error_log('fetch_object() failed');
return false;
}
$id = $result->id;
For example, for some queries like SELECT MAX(field), the query result is usually only a field value, rather than returning rows to you.
Now the field of the value I wanna get is integer type.
As I'm a beginner of php, how can I get that value from the query result?
As I do the following
$query = "SELECT MAX(stringid) FROM XMLString";
$result = mysql_query($query, $link);
echo $result;
Then nothing is echoed out.
I have check the db connection made by mysqlconnect, and it's got no problem.
And I tried this query in MySQL at phpMyAdmin, then the query is what I want, too?
So why would it be like that, and any solution?
Thanks!
You will always retrieve rows back from an SQL query, even if there's only one row with one field. You can directly retrieve a specific field of a specific row using mysql_result:
$query = "SELECT MAX(stringid) FROM XMLString";
$result = mysql_query($query, $link);
echo mysql_result($result, 0, 0);
$query = "SELECT MAX(stringid) as val FROM XMLString";
$result = mysql_query($query, $link);
$rows = mysql_num_rows($result);
if($rows > 0) {
$rstAry = mysql_fetch_array($result);
echo $rstAry['val'];
}
Try This
mysql_query is not printing results. Maybe try this:
echo mysql_result($result);
You are not getting any result because mysql_query returns a database object.
You will need to process this "database object" using mysql_fetch_array. This command 'fetches' an array out of a MySQL "database object". The array you are fetching is the rows your query produced. In your case, it is just one row.
Here is the code:
This step sets your query:
$query = "SELECT MAX(stringid) as val FROM XMLString";
This step will run your query, and return the database object:
$result_database_object_whatever = mysql_query($query, $link);
This step will process the database object, and give you an array of the queried rows:
$result_array = mysql_fetch_array($query, $link);
This step will echo the first row returned by your query:
echo $result_array[1];
You can do something like this:
$query = "SELECT MAX(stringid) FROM XMLString";
$result = mysql_query($query, $link);
$fetch = mysql_fetch_assoc($result);
echo $fetch['stringid'];
The mysql_fetch_assoc() function retrieves the data in an associative array.
Would it help to give the max a name you could reference? Also ifyou think there's syntax errors you could just add the error to help clarify.
$query = "SELECT MAX(stringid) as maxNum FROM XMLString";
$result = mysql_query($query, $link) or die(mysql_error());
echo $result;