Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I dont know what is the problem with this function:
function server_grafico_expirar($data) {
$data = sanitize($data);
$query = mysql_query("SELECT `data_exp` FROM `settings` WHERE `data_exp` = '$data'");
return (mysql_result($query, 0) == 1) ? true : false;
}
This function was supposed to return true if the date('d.m.Y') equals the date in mysql.
Im using like this:
$data = date('d.m.Y');
if(server_grafico_expirar($data)){
echo "Today, is the date!";
}
The error is:
Parse error: syntax error, unexpected T_IF in /home/mcser325/public_html/checker.php on line 35
Firstly, you must make sure that the settings table does contain a row that has the data_exp column set to todays date in the format d.m.Y.
mysql_result retrieves the contents of one cell from a MySQL result set. The cell that you are retrieving is data_exp. From your question I have assumed that data_exp is a date in the format of d.m.Y.
With that said, mysql_result($query, 0) will never be equal to 1 as it will return the date you are selecting. You could approach this in two ways, you could either check if the cell equals $data and then return true
function server_grafico_expirar($data) {
$data = sanitize($data);
$query = mysql_query("SELECT `data_exp` FROM `settings` WHERE `data_exp` = '$data'");
return mysql_result($query, 0) == $data;
}
You could also check how many rows are returned. If more than zero rows are returned then you can return true.
function server_grafico_expirar($data) {
$data = sanitize($data);
$query = mysql_query("SELECT `data_exp` FROM `settings` WHERE `data_exp` = '$data'");
return mysql_num_rows($query) > 0;
}
Please note that the mysql 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.
Aside from any other potential errors, what your PHP parser is saying is that there is a syntax error. It's not in what you showed us at least, but make sure that you've closed every corresponding curly brackets ({ }) and they are properly matched up and that all lines end with a semicolon (;).
I'd suggest indenting properly to find the error more easily.
Related
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
The following error is appearing in my code
Warning: pg_num_rows() expects exactly 1 parameter, 0 given in /var/www/funcoes.php on line 52
This is the code of the function:
function cadastrarFornecedor($nome){
$qry = "INSERT INTO public.fornecedor (nome) VALUES ('".$nome."')";
$result = pg_query($qry);
if(pg_num_rows() > 0){
return 1;
}
else{
return 0;
}
}
pg_num_rows takes a result-set variable as an argument. The error is telling you that an argument is expected but none was provided.
See the unction signature: int pg_num_rows ( resource $result )
Where, $result:
PostgreSQL query result resource, returned by pg_query(),
pg_query_params() or pg_execute() (among others)
So your code should pass the query result set to pg_num_rows, like the example below:
function cadastrarFornecedor($nome){
$qry = "INSERT INTO public.fornecedor (nome) VALUES ('".$nome."')";
$result = pg_query($qry);
if(pg_num_rows($result) > 0){
return 1;
} else{
return 0;
}
}
Source: http://www.php.net/manual/en/function.pg-num-rows.php
(Note that pg_num_rows will return zero if you insert into a view with a DO INSTEAD trigger, a table with partitioning triggers/rules, etc.)
Warning: pg_num_rows() expects exactly 1 parameter, 0 given in /var/www/funcoes.php on line 52
The error speaks for itself
function cadastrarFornecedor($nome){
$qry = "INSERT INTO public.fornecedor (nome) VALUES ('" . $nome . "')";
$result = pg_query($qry);
if (pg_num_rows($result) > 0) {
return 1;
}
else {
return 0;
}
}
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 keep getting error:
[24-Nov-2013 02:13:58] PHP Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home5/mwtwoone/public_html/sw6/ebay/test.php on line 17
The error is referring to the line mysql_query($con,"UPDATE codes SET multi = '1' WHERE code == $code_array[0]");
<?php
$con=mysql_connect(Localhost,"mwtwoone_xbl","223822","mwtwoone_xbl");
mysql_select_db( 'mwtwoone_xbl' );
$sqlcodes = "SELECT `code` FROM `codes` WHERE `sent` = 0";
$rawcodes = mysql_query($sqlcodes); // process the query
$code_array = array(); // start an array
while($row = mysql_fetch_array($rawcodes)){ // cycle through each record returned
$code_array[] = $row['code'];
}
echo $code_array[0]; // output the string to the display
mysql_query($con,"UPDATE codes SET multi = '1' WHERE `code` == $code_array[0]");
mysql_close($con);
?>
I've been messing with this for all now and cant seem to fix it, thanks for all the help.
Try using, use = instead of == and $con should be mysql_query($query, $con)
mysql_query("UPDATE codes SET multi = '1' WHERE `code` ='".$code_array[0]."' ");
instead of
mysql_query($con,"UPDATE codes SET multi = '1' WHERE `code` == $code_array[0]");
Syntax: resource mysql_query ( string $query [, resource $link_identifier = NULL ] )
I am not sure if I am correct. in this structure you don't need 2 equal signs "== " just one is enough. give it a try .
First of all, you need to use mySQLi or PDO, mySQL is deprecated.
This should work.
mysql_query("UPDATE table1 SET column1 = 'value1' WHERE `column2` ='".$array[0]."' ");
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 used this code for displaying 4 numbers randomly without repeating, but i got error as resource id#3
$test = nonRepeat(0,4,4); //calling function nonrepeat defined earlier
for ( $i = 0; $i < 4; $i++ ) {
$result = mysql_query( "select * from abc where id='test[i]'" ); accessing data from data base as id in the array test.
print_r( $result );
}
1.- Don't use mysql_* functions they are deprecated and will not be included in future updates.
2.- Your question is pretty unclear, but when you execute a query using mysql_query you get a resource, so you need to iterate through this:
$test = nonRepeat(0,4,4); //calling function nonrepeat defined earlier
for($i=0;$i<4;$i++)
{
$result = mysql_query( "select * from abc where id='{$test[i]}'" ); <<<---- CHANGED
if ($result){
while ($row = mysql_fetch_assoc($result)) {
print_r($row); //Display each row data
}
}else{
print "Error:" . mysql_error();
}
}
Try this and see what is showing, and take a look to mysqli_ and PDO
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
function getById($id) {
global $mysqli;
$result = $mysqli->query("SELECT * FROM `ids` WHERE `id` = '".$mysqli->real_escape_string($id)."'") or die($mysqli->error);
$rows = array();
while($row = $result->fetch_array(MYSQLI_ASSOC)) {
$rows[] = $row;
}
return $rows;
}
How do I get the value of id field?
$data = getById(1);
echo $data[0] or echo $data['id'] doesn't work.
Your function returns an array of arrays, so to get the first ID:
$data = getById(1);
echo $data[0]['id'];
Side notes:
Globals are undesirable. Consider passing the database connection object to the function as an argument instead.
Escaping like that is a primitive way of preventing SQL Injection. The use of a Prepared Statement would be more secure and easier.
or die(....) is bad practice because it exposes the detailed MySQL error to users, and it's hard to remove if you have written that hundreds of times. An alternative is trigger_error() which silently writes to your error log.
You no need to use while and array. Get single row and you can get without using array:
function getById($id) {
global $mysqli;
$result = $mysqli->query("SELECT * FROM `ids` WHERE `id` = '".$mysqli->real_escape_string($id)."'") or die($mysqli->error);
return $result->fetch_array(MYSQLI_ASSOC);
}
$data = getById(1);
echo $data['id'];
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
So I have this code:
<?php
if (!isset($_GET['email']) && !isset($_GET['key'])) {
echo 'NOT ALLOWED TO ACCESS PAGE';
}
if (isset($_GET['email']) && isset($_GET['key'])) {
$email = $_GET['email'];
$regKey = $_GET['key'];
$query = mysql_query("SELECT * FROM `users` WHERE `email`='$email' AND `regRandom`='$regKey'");
if ($query == $regKey) {
mysql_query("UPDATE `users` SET `activated`=1 WHERE `email`='$email'");
if (mysql_query("SELECT activated FROM users WHERE activated='1'")) {
echo file_get_contents("http://TheMegaHouse.com/pageContent/head.php");
echo file_get_contents("http://TheMegaHouse.com/pageContent/notLoggedIn/topNavBar.php");
echo file_get_contents("http://TheMegaHouse.com/pageContent/header.php");
echo 'PASSED';
footer();
} else {
activationFail();
}
} else if ($regKey !== $query) {
echo 'FAIL';
}
}
?>
The thing is that in my database, the value that is in the regRandom column is in fact equal to the value in the key variable in the url but for some reason it is echoing out "FAIL" instead of "PASSED".
Any suggestions on what I am doing wrong, a better way of doing what I want to do or any suggestions?
Always read manual first:
http://php.net/manual/en/function.mysql-query.php
This line of code:
mysql_query("SELECT * FROM `users` WHERE `email`='$email' AND `regRandom`='$regKey'");
Is returning you resource, instead of variable you are lookng for.
Small fix for you:
list($regRandom) = mysql_fetch_row(mysql_query("SELECT regRandom FROM `users` WHERE `email`='$email' AND `regRandom`='$regKey'"));
if ($regRandom == $regKey) {
// magic
And another two things:
mysql_* functions are deprecated
i strongly recommend you to read about SQL Injections.
You should not be using the deprecated mysql_* functions but switch to PDO or mysqli and prepared statements.
However, the problem you are having is that you are comparing a resource to a sent-in variable.
My guess is that $regKey is an integer and $query is the result of a mysql query. You need to fetch a row from the result-set and compare a specific value from that row to your $regKey.
Correct me if Im wrong, but $query results is a resource? You haven't done anything with it.
I would suggest just doing a simple mysql_num_rows() check like this:
$query = mysql_query("SELECT * FROM `users` WHERE `email`='$email' AND `regRandom`='$regKey'");
if (mysql_num_rows($query) == 1) {
Its pointless to check again because you are already checking in the SQL query :) So simply check the number of rows returned. 0 = not found, 1+ = results :)