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)
Related
I have a database table that has 4 records with a column _id that auto increments. When I run a query to get all records, it works but it doesn't echo out all the ids, it only points to the first rows and echos it four times. I am using PHP and MySQLi. Here is my code
Code for querying
$sql = "SELECT * FROM att_table";
$query = $conn->query($sql);
$result = $query->fetch_assoc();
Code for display
do{
echo result['_id'];
}while($query->fetch_assoc());
It outputs 1111 instead of 1234. Please what is wrong?
You're fetching each of the 4 results, so it loops the appropriate number of times; but you're only assigning the fetched result to $result once, so that's the only _id value that gets echoed
do{
echo $result['_id'];
}while($result = $query->fetch_assoc())
You also can use a foreach loop :
$sql = "SELECT * FROM att_table";
$query = $conn->query($sql);
$result = $query->fetch_assoc();
foreach($result as $data){
echo $data['_id'];
}
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);
When I try to mysql_fetch_row the array that is created contains 2 fields from my selection at each index. I would like to ask why is this happening?
<?php
$categoryid = $_GET['id'];
include('connect.php');
$query = "SELECT
Categories_SubCategories.IdCategory,Categories_SubCategories.idSub_Category,
Categories.Name, Sub_Categories.Name from Categories_SubCategories JOIN
Categories on Categories.idCategory = Categories_SubCategories.idCategory
JOIN Sub_Categories on Categories_SubCategories.idSub_Category =
Sub_Categories.idSub_Category
WHERE Categories_SubCategories.IdCategory = $categoryid";
$result = mysql_query($query);
$rows = mysql_num_rows($result);
for($i=0; $i<$rows; $i++){
$display = mysql_fetch_row($result);
echo "$display[3]";
}
?>
most PHP programmers use while() loop when they want to work with mysql_fetch_array().
Take a look at this sample code:
$query = mysql_query("SELECT id,name FROM tbl_members");
if (mysql_num_rows($query)) {
while ($result = mysql_fetch_array($query)) {
echo('User #'.$result['id'].' is: '.$result['name'].'<br />');
}
}
// Output can be something like this:
// User #1 is: John
// User #2 is: Sarah
replace mysql_num_rows with mysql_fetch_array or mysql_fetch_assoc
I'm working on a type of spellcheck, autosuggest. I have two dictionaries, one with phrases, and one that should kick in for basic spelling suggestions. Right now I have this on the server side:
$q = strtolower($_GET["q"]);
$q= mysql_real_escape_string($q);
if (!$q) return;
$sql = ("SELECT headings FROM dictionary WHERE headings LIKE '$q%' LIMIT 3");
$rsd = mysql_query($sql);
while($rs = mysql_fetch_array($rsd)) {
$auto = $rs['headings'];
echo "$auto\n";
}
if (mysql_num_rows(mysql_query("SELECT headings FROM dictionary WHERE headings LIKE '$q%' LIMIT 3")) == 0){
$res = mysql_query("SELECT spelling FROM spellcheck WHERE spelling LIKE '$s%' LIMIT 3");
while($result = mysql_fetch_array($res)) {
$spell = $result['spelling'];
echo "$spell\n";
}
}
basically, $s should equal where the first query left off. So if the query was : hello wor. $s would be wor and it would suggest "world" from the mysql_query: $res. I would have to deal with multiple spaces between words too.
Use mysql_affected_rows() to get affected rows of last executed query...
refer http://php.net/manual/en/function.mysql-affected-rows.php
I think my mind is just drawing a blank, but basically, I want to create an associative array from various sql results
The array needs to look like:
$people = array(
"+1123456789" => "Phil"
);
Here is my SQL Statement
$sql = " SELECT phonenumber6, firstName FROM members WHERE departmentID = 4 AND phonenumber6 <> '+1';";
Thanks!
Edit:
Also, there can be multiple rows that were selected by the sql statement
$sql = " SELECT phonenumber6, firstName FROM members WHERE departmentID = 4 AND phonenumber6 <> '+1';";
$result = mysql_query($sql);
while($row=mysql_fetch_assoc($result)) {
echo $people[$row['phonenumber6']] = $row['firstName'];
}
while($row=mysql_fetch_assoc($query)) {
$people[$row['phonenumber6']] = $row['firstName'];
}
Addendum
Dunno what you want to echo. Anyway the right syntax is:
while($row=mysql_fetch_assoc($query)) {
$people[$row['phonenumber6']] = $row['firstName'];
echo $row['phonenumber6']. '=> '.$row['firstName']."<br />\n";
}
This will give you an associated array from a mysql result set:
$assoc = mysql_fetch_assoc ($res);