I am reading values from excel sheet in $number_column variable
$number_column = $data->sheets[0]['cells'][$row1][1];
Then make a select query
$query_patent = "SELECT `id` FROM `ipoverview` WHERE number ='$number_column'";
It is not working. If I echo my $number_column I am getting the data at that location.
And if hard code any number to $number_column, that query is executing.
$number_column="12345";
echo $query_patent = "SELECT `id` FROM `ipoverview` WHERE number ='$number_column'"; //This works
Help me figure out this issue??
Make sure that you have UTF-8 as a character encode for your excel file,
and also make sure that you should have utf8_general_ci as collation for your database column
for more info please refer http://docs.php.net/manual/en/mysqli.set-charset.php.
thanks
You've got this line:
echo $query_patent = "SELECT `id` FROM `ipoverview` WHERE number ='$number_column'";
shouldn't that be something like this:
$query_patent = "SELECT `id` FROM `ipoverview` WHERE number ='$number_column'";
$rResult = mysqli_query($dblink,$query_patent)or die("something went wrong when executing this query:<br /><br />" + $query_patent + "<br /><br />"+ mysqli_error($dbLink));
// Dot Stuff with $rResult
while($row = mysqli_fetch_row($rResult)){
print_r($row);
}
Try this.And also echo $query_patent whether you are getting a variable on that variable before executing the query..
echo $query_patent = "SELECT `id` FROM `ipoverview` WHERE number ='".$number_column."'";
The response of the above echo
SELECT `id` FROM `ipoverview` WHERE number = 'E1234A'
SELECT `id` FROM `ipoverview` WHERE number = 'E1234B'
SELECT `id` FROM `ipoverview` WHERE number = 'E1234C'
Related
I want to update data row
But the problem is that it is updated All rows
this is my code:
$query = mysql_query("SELECT *
FROM emp
JOIN inv ON emp.name=inv.empname
WHERE inv.empname='".$name."'")
or die ("mysql error query");
$id = $_POST['id'];
$updatestartdate = date('d/m/Y');
$updateenddate = date('d/m/Y');
$status = $_POST['status'];
while ($rowshow = mysql_fetch_assoc($query)){
$timetoshow = unix_time($rowshow['timex']);
//UPDATE
if (isset($_POST['Update']) and $_POST['Update'] == 'dataupdate'){
$updatestatus = mysql_query ("UPDATE inv SET
updatestartdate='$updatestartdate',
updateenddate='$updateenddate',
status='$status'
WHERE id='".$rowshow['id']."'")
or die ("updatestatus Error");
if (isset ($updatestatus)){
echo "<div class='hidecontent'><h3 style='background-color:#3F3F3F; padding:5px;' align='center'>
<font color='#FFFFFF'>Update is done</font></h3><meta http-equiv='Refresh' content='5; url=cpanel_user.php' /></div>";
} else {
echo "<div class='hidecontent'><h3 style='background-color:#FF0000; padding:5px;' align='center'>
<font color='#FFFF00'>Update Error</font></h3></div>";
}
}
Where is problem?
If it's updating all the rows then it probably means that you are not filtering the data which is to be updated. To filter the data, use the WHERE clause. Next ensure that all rows don't have a duplicate values else it would update all the rows as well. Consider the following code:
UPDATE `database` . `table` SET `Name` = 'Zahid Saeed' WHERE `Name` = 'Zahid';
The point is, the person named 'Zahid' is not alone in this world. You have to be more specific. If you can, always use the ID column to update a value as it is always unique just like so:
UPDATE `database` . `table` SET `Name` = 'Zahid Saeed' WHERE `id` = 'Some ID'
If you can't get the ID, use the AND clause to match multiple columns to filter the data more accurately like this:
UPDATE `database` . `table` SET `Name` = 'Zahid Saeed' WHERE `Name` = 'Zahid' AND `Age` = 19 AND `Email` = 'abc#hotmail.com'
Your query should be
"UPDATE inv
SET updatestartdate='$updatestartdate',
updateenddate='$updateenddate',
status='$status'
WHERE id = '".$id."'";
I'm new to both PHP & mySQL, but I suspect some apostrophe related bug here (maybe).
For some reason the first query always seems to return null because the
echo "result: $result\n";
never prints any data. At the first call that is expected, but at the second call the player has been added to the db. My database works in the sense that I can see that rows with correct ids are added to the database (via phpMyAdmin).
Can you help me spot the error?
<?php
require_once('Db.php');
$db = new Db();
// Quote and escape form submitted values
$id = $db->quote($_POST['id']);
$score = $db->quote($_POST['score']);
$result = $db->query("SELECT `id` FROM `xxxxxx`.`Player` WHERE `id` = `$id`");
echo "result: $result\n"; // Never prints any data
if($result->num_rows == 0) {
// Row not found. Create it!
$result = $db->query("INSERT INTO `xxxxxx`.`Player` (`id`,`score`) VALUES (" . $id . "," . 0 . ")");
}
?>
First, drop those backticks from id in WHERE clause, otherwise it will take the field name from id column instead of 'id'.
Then you need to fetch data from $result:
$result = $db->query("SELECT id FROM `xxxxxx`.`Player` WHERE id = '$id'");
$row = $result->fetch_array();
echo $row['id'];
Or if there are more rows than one:
while($row = $result->fetch_array())
{
echo $row['id'];
}
You are using backticks in your query for $id. Remove them and try again.
Your query should be
$result = $db->query("SELECT `id` FROM `xxxxxx`.`Player` WHERE `id` = $id");
OR
$result = $db->query("SELECT `id` FROM `xxxxxx`.`Player` WHERE `id` = ".$id."");
I want to count the total comment/post posted on my page. i have a table in my database named test. within table i have a column named comment, where every post is been stored. the problem am having is to echo out the total number of comment and keep updating as viewers keep on posting there comment and i tried using this code
<?php
$handle = mysql_query("SELECT `comment`, COUNT(*) AS `count`
FROM test GROUP BY `comment` ");
if ($handle) {
$results = mysql_fetch_assoc($handle);
echo ($results[0]['count'] + $results[1]['count']);
}
?>
but it keep on echoing out 0. pls help me out.
Try this:
list($count) = mysql_fetch_row(mysql_query("select count(*) from `test`"));
echo $count;
Alternatively, if you are already running a query to get some comments, you can try this:
$sql = mysql_query("select sql_calc_found_rows * from `test` order by `id` desc limit 10");
// ^ Get the 10 most recent comments
list($count) = mysql_fetch_row(mysql_query("select found_rows()"));
// this avoids having to run the entire query again, great for efficiency!
while($comment = mysql_fetch_assoc($sql)) var_dump($comment); // just an example
I'm trying to display a field from my MySQL database. It's in the table tblproducts in the row with the id is set to 1. And under the column qty.
This is the code I'm using:
<?php
mysql_connect("localhost","username","password");
mysql_select_db("database_name");
$available = "SELECT qty FROM tblproducts WHERE id = 1";
$result = mysql_query($available);
echo $result;
?>
However, I keep getting this message: Resource id #2
I've done a bit of research and seen where other people are having similar problems but most of them are trying to display their data in an HTML table whereas I just need the data from 'qty' to display. And of course I'm definitely not a MySQL guru.
Can anyone help me out with this please?
Try changing this:
$result = mysql_query($available);
To this:
$result = mysql_result(mysql_query($available), 0);
Let's start from the start. (I'll assume you have the connection set)
Form the query
$query = "SELECT `qty`
FROM `tblproducts`
WHERE `id` = 1";
Execute the query
$run = mysql_query($query);
Now, put the result in an assoc array
$r = mysql_fetch_array($run);
See the contents of the array
echo $r['qty'];
It's also advised that you move up from mysql to either mysqli, or PDO. PDO is preferred as you're not bound to the MySQL database model.
Try this:
Here you need to generate associative array and then get the resulting row.
$query = "SELECT `qty` FROM `tblproducts` WHERE `id` = 1";
$run = mysql_query($query);
$r = mysql_fetch_array($run);
echo $r['qty'];
-
Thanks
Newish to mysql. I have a query and it is not showing the value of the cell just the row name:
$sql="SELECT 'first' from `users` WHERE `id`= $userid ";
$res=mysql_query($sql) or die(mysql_error());
$row=mysql_fetch_assoc($res);
echo $row['first'] ;
What am I doing wrong????
Brackets in your query is wrong:
$sql = "SELECT 'first' from `users` WHERE `id` = $userid";
Must be:
$sql = "SELECT `first` from `users` WHERE `id` = $userid";
Note difference in first
First remove quotes from 'first' - it is a column so don't put it in quotes, you can use ` istead.
Next loop through results and that's all.
$sql="SELECT first from `users` WHERE `id`= $userid ";
$res=mysql_query($sql) or die(mysql_error());
while($row=mysql_fetch_assoc($res))
echo $row['first'] ;
Try:
echo $row[0]['first'];
SELECT 'first'
will simply return the string first.
remove the quotes.
$sql="SELECT 'first' from users WHERE id= $userid ";
you are using normal quotes to select instead of backticks you are not selecting anything from the database.
use
$sql="SELECT first from users WHERE id= $userid ";
instead
and side note:
never "be sure" that your query returns exactly 1 row
use mysql_fetch_assoc() in a loop and check if you really retrieve 1 result.