I've been on this for some days now. At first I thought the problem was in binding the parameters but I've simplified back to a basic mysqli page and still can't find the error. I'm passing the key for one of the rows in the search page before this onto this page so that I can show more details of the item which was selected.
I added an echo to test the the isset which prints correctly, also it puts the Key into the URL. If I leave out the WHERE Key = '$Key' it prints out the entire dataset. If I replace $row['Key'] with $Key it prints the whole dataset but with the selected key on every row.
This tells me that it is passing the key correctly and the print function is correct. I've tried using WHERE Key = $_GET['Key'] as well as $Key but neither work. I must be doing something basicly wrong here but after three days of trying every variation on the code I can think of, I have no more ideas.
<?php
$mysqli = new mysqli('localhost','user','password','database');
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
if(isset($_GET['Key'])){
$Key = $_GET['Key'];
echo "Got it";
}else{
echo "No input";
}
$results = $mysqli->query("SELECT * FROM engravers WHERE Key ='$Key'");
$img_url = "http://www.xxxxx.net/images/";
print '<table border="1" >';
while($row = $results->fetch_assoc()) {
print '<tr>';
print '<td>'.$row["Key"].'</td>';
print '<td>'.$row["Country"].'</td>';
print '<td>'.$row["Year"].'</td>';
print '<td>'.$row["Description"].'</td>';
print '<td>'.$row["Engraver1Surname"].'</td>';
print '<td>'.$row["Designer1Surname"].'</td>';
print '<td>'.$row["Printer"].'</td>';
print '<td>'.'<img src="'.$img_url.$row['Images'].'" />'.'</td>';
print '</tr>';
}
print '</table>';
$results->free();
$mysqli->close();
?>
</body>
There are many SQL column names you should avoid. Please read: http://technet.microsoft.com/en-us/library/ms189822.aspx
Same Reserved Keywords are in MySQL.
If you use one of those just cover it with ``
$results = $mysqli->query("SELECT * FROM `engravers` WHERE `Key`='$Key'");
In your query you are using column key which is not allowed because this is a reserved keyword.
Man, you're already using mysqli, why are you still interpolating variables in your query? This is the way to pass a string $Key to the parser
$results = $mysqli->prepare("SELECT * FROM engravers WHERE `Key`= ?");
$results->bind_param("s", $Key);
while($row = $results->fetch_assoc()) {
... stuff ...
}
If that doesn't work, I'm sure it's not because the prepared statement is the problem.
PD: key is a reserved word, so please note the fieldname is wrapped in backticks.
Related
I'm learning this new langage PHP in order to develop modules from this software : Dolibarr
It's the first time I'm using PHP and I don't overcome to display query result in my view.
I would like to know if I wrote something wrong in my script because I don't understand all up to now. I would like to display the number of users in my software. I have to query my llx_user table and display the result in my array.
This is the part of my code :
/*
* View
*/
//Display number of users
$sql = "SELECT COUNT(u.rowid) as total";
$sql.= " FROM ".MAIN_DB_PREFIX."user as u";
$result = $db->query($sql);
print '<table class="noborder" width="100%">';
print '<tr class="liste_titre"><th colspan="2">'.$langs->trans("Statistics").'</th></tr>';
if (! empty($conf->user->enabled))
{
$statUsers = '<tr class="oddeven">';
$statUsers.= '<td>'.$langs->trans("Number of Users").'</td><td align="right">'.round($result).'</td>';
$statUsers.= "</tr>";
}
$total=0;
if ($entity == '0')
{
print $statUsers;
$total=round($result);
}
print '<tr class="liste_total"><td>'.$langs->trans("Total").'</td><td align="right">';
print $total;
print '</td></tr>';
print '</table>';
print '</div></div></div>';
llxFooter();
$db->close();
As I said, it's the first time I'm handling php file and I began to learn php 3 hours ago.
This is what I got :
If I comment like this :
$total=0;
//if ($entity == '0')
//{
print $statUsers;
$total=round($result);
//}
I'm getting this :
But I have 2 users in my table :
Thank you if you could help me
You're doing a good job for that you just started with PHP. Anyway, there's a little mistake in your code.
You actually query the database, but you don't fetch the result.
You have to do the following after your query:
$row = $result->fetch_row();
print $row[0]; // $row[0] will contain the value you're looking for
Also it seems that your $entity is not equal to 0. I don't see you initializing this variable anywhere, are you sure you have defined it? May you want to show us some mor of your code..
In this application I have a drop down so you can select what table you want to view. Using Ajax I push the variable $tbl as an integer and the code below prints out the table that corresponds with that integer. Something is not working though and I need an extra pair of eyes to help debug.
if($tbl == 9){$table = "person";}
$conn = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME);
$query = mysqli_query($conn, "SELECT * FROM $table ") or die(mysqli_error($conn));
if($query){
echo '<table border="1"><thead><tr>';
$colnames = array();
while ($finfo = mysqli_fetch_field($query)) {
$name=$finfo->name;
array_push($colnames, $name);
echo "<th>".$name."</th>";
}
mysqli_free_result($query);
echo '</tr></thead><tbody>';
$count = 0;
echo $colnames[$count]; // this test prints the correct column name but it ends up being printed OUTSIDE the table for some reason
while ($row = mysqli_fetch_array($query, MYSQLI_BOTH)){
echo '<td>'.$row[$colnames[$count]].'</td>';
$count++;
}
echo "</tbody></table>";
}
The last while loop is suppose to echo out each field value but it isn't executing <tbody> is left blank.
You called mysqli_free_result($query) too early. So by the time you called mysqli_fetch_array, the sql result was already gone. Move mysqli_free_result after the last loop.
I'm not sure whether it's a typo or that the following is where the problem lies, but you're missing opening and closing <tr> tags around the column names after <tbody> and before </tbody>. This may help explain why stuff is being printed apparently "outside" the table.
I got this list of checkboxes that print out from database. I am able to retrieve data from database if user makes multiple checkbox and display it in table, but i cannot retrieved other information from database and display it in the same table.
This is my code:
<table border='1'>
<tr>
<th>TITLE</th>
<th>PERCENTAGE RESULT</th>
</tr>
<?php
if(isset ($_POST["submit1"]))
{
$selectedcheckbox = $_POST["selectedcheck"];
$query = "SELECT * FROM compareresult where subject=$selectedcheckbox";
$sql_query = mysql_query($query) or die('Error 3 :'.mysql_error());
while($data = mysql_fetch_array($sql_query,MYSQL_ASSOC)){
$result=$data['result'];
}
foreach($selectedcheckbox as $title)
{
echo "<tr>";
echo "<td>".$title."</td>";
echo "<td>".$result."</td>";
}
echo "</tr>";
}
?>
I want to display result after user select multiple checkbox, so I wrote:
echo $result in table
so that the result can be displayed in table beside the selected title, but I am getting an error:
Array to string conversion in C:\xampp\htdocs\sam\c.php on line 31 Error 3 :Unknown column 'Array' in 'where clause'
I am not at the moment in the environment to test your problem, and I do this out of my head, but try something like the following.
if(isset($_POST['submit1'])){
$checkbox = isset($_POST['selectedcheck']) ? $_POST['selectedcheck'] : array();
foreach($checkbox as $title){
$query = "SELECT * FROM compareresult where subject='".$title."'";
$result=mysql_query($query); // <-- to avoid SQL injections, please change your method from mysql_query to mysqli_query (use the mysqli functions instead of mysql)
while($data = mysql_fetch_array($result)){
$result=$data['result'];
echo "<tr>";
echo "<td>".$title."</td>";
echo "<td>".$result."</td>";
echo "</tr>
}
}
}
It checks first if your checkboxes are checked and if they are they put it in an array.
Then it runs the foreach statement, where it sets the checkbox value as title. It runs there if the query if it the title is in the database, if so, spit it out through the while loop. Rinse and repeat until done.
edit
I see you put your open tablerow statement in the foreach but close it outside. So either you want it all printed on one line or is it an error? If its on one line, make sure you open the table row BEFORE the while loop and end it AFTER, else it's like how i spit it out.
Here is my code:
$campagin_id = $_SESSION['campagin_id_for_camp'];
$query = "SELECT * FROM survey_result where campagin_id = ".$campagin_id;
$conn=mysql_connect($dbconfig['db_hostname'],$dbconfig['db_username'],$dbconfig['db_password']) or die(mysql_error());
mysql_select_db($dbconfig['db_name'],$conn);
$exec_query =mysql_query($query) or die(mysql_error());
$row=mysql_fetch_array($exec_query);
echo "<br> row = ".$row;
while ($row=mysql_fetch_array($exec_query)){
echo "I am In";
}
The Problem is that I am not getting anything in $row I cant get into the while loop, nothing shows up when I try to echo the value of $row, No error Nothing. Can you help me to find a problem in my code ?
Ps : The database is their. I have checked for the query for the corresponding value of $campagin_id. and also when i tried to echo $exec_query it echoed this : Resource id #8
PPS : The database have more than 7 record for each id so it doesn't matter if I call mysql_fetch_array($exec_query) more than once before going in to the while loop. and for the $campagin_id in the session their are many records present in the database.
You have written $row=mysql_fetch_array($exec_query) and then you are echoing something. and you are using the same in while.
Instead of:
$row=mysql_fetch_array($exec_query);
echo "<br> row = ".$row;
while ($row=mysql_fetch_array($exec_query)){
echo "I am In";
}
Use this (as per my knowledge you should not use $row=mysql_fetch_array() once you have used before while):
while ($row=mysql_fetch_array($exec_query)){
echo "I am In";
}
If the query returns Resource id #8 then that means it was successful - ie there were no errors. There were probably no rows returned by that query, so no rows in your table that match the given campagin_id.
You are also calling mysql_fetch_array() twice separately, you shouldn't do that because your while loop will skip the first row because calling this moves the pointer in the result set forward by one.
Also you can't echo an array as you are trying to, if you want to see the contents of an array use print_r() or var_dump().
I suggest adding some code to handle no rows found:
if($exec_query && mysql_num_rows($exec_query) > 0)
{
while ($row=mysql_fetch_array($exec_query)){
echo "Row: " . print_r($row, true);
}
}
else
{
echo 'None found';
}
Try this code.
<?
$campagin_id = $_SESSION['campagin_id_for_camp'];
$query = "SELECT * FROM survey_result where campagin_id = ".$campagin_id;
mysql_connect($dbconfig['db_hostname'],$dbconfig['db_username'],$dbconfig['db_password']) or die(mysql_error());
mysql_select_db($dbconfig['db_name']);
$exec_query =mysql_query($query) or die(mysql_error());
while ($row=mysql_fetch_assoc($exec_query)) {
echo "<br/> row = <pre>".print_r($row)."</pre><br/>";
}
?>
I wrote the following code to select text from database,but when i echo the output it giving output as Resource id #4
mysql_select_db("xxxxx", $link);
$q = "SELECT start_of FROM `qr_table` WHERE id_qr =1";
$result = mysql_query ($q, $link);
echo $result;
i am new to sql,forgive me if its a stupid questain
Thanks in advance
I suggest you to read at least Php documentation about Mysql query function.
you are echoing out the connection. you need to do something with the results like loop through them please check http://www.php.net/manual/en/function.mysql-query.php
You can't print out the result from MySQL directly. Try mysql_fetch_assoc(), which loads the value of each column into an associative array. if you have multiple rows returned, it will move to the next one each time it is called, and return false when there are no more.
this help for you
<?php
$link=mysql_pconnect("localhost","root","")or die("Not connected".mysql_error());
mysql_select_db("test");
$query="select * from qr_table";
$result=mysql_query($query,$link)or die("Query failed".mysql_error());
print "<center><table border=1>\n";
while($line=mysql_fetch_array($result,MYSQL_ASSOC)){
print "\t<tr>\n";
foreach($line as $col_value){
print "\t\t<td>$col_value</td>\n";
}
print "\t</tr>\n";
}
print "</table>\n</center>";
?>