Get record by id - php

Here is my code:
case 'records': $this-> get_records ($callParams[1]);
private function get_records($id)
{
$result = get_records_info ($id) ;
if(count($result) > 0)
$this->response($this->text/html($result), 200);
else
$this->response('',204);
}
function get_records_info (){
$result = mysql_query ("SELECT * FROM `records` ")
or die(mysql_error());
while($records = mysql_fetch_array( $result )) {
echo "<div>" .$records['records_name']. "</div>";
echo "<div>info:" .$records['w']. $records['l']. $records['d']. $records['k']."</div>";
echo "<div>info2:".$records['info2']."</div>";
}
}
Here is what I'm trying to do:
When you click on records is goes to domain.com/record/id and only displays the record of that id.
Here is what is happening:
I got it to work but I'm getting all the records in the database.

Your result is currently:
mysql_query ("SELECT * FROM `records` ")
This is going to grab all the records regardless. What you want is something like this:
mysql_query ("SELECT * FROM `records` WHERE `id` = '$id' ")
$id being the supplied id to fetch the record for.
Note: As stated in the comments, stay away from mysql_* functions as it is depreciated. Look into PDO or MySQLi :)

try this.... You are passing the id in function but not using thats why it is showing you all recrods, pass id in function function get_records_info ($id) then in query .
<?php
//case 'records': $this-> get_records ($callParams[1]);
case 'records': $this-> get_records ($id); //just pass the id of the user here
private function get_records($id)
{
$result = get_records_info ($id) ;
if(count($result) > 0)
$this->response($this->text/html($result), 200);
else
$this->response('',204);
}
function get_records_info ($id){
$result = mysql_query ("SELECT * FROM `records` where `id` = ' ".$id." ' ")
or die(mysql_error());
while($records = mysql_fetch_array( $result )) {
echo "<div>" .$records['records_name']. "</div>";
echo "<div>info:" .$records['w']. $records['l']. $records['d']. $records['k']."</div>";
echo "<div>info2:".$records['info2']."</div>";
}
}
?>

Related

Count and echo the amount of users that have registered [duplicate]

I would like to echo the number of people registered on my website
only the code that I have does not work, it gives me back that it can't be
converted to string. Also when I make it a function to call in my HTML I get error that $connection is undefined
require_once("connect.php");
$sql = "SELECT * FROM persons";
if ($result=mysqli_query($connection, $sql)){
$rowcount = mysqli_num_rows($result);
mysqli_free_result($result);
return $result;}
How do I get this in a function that I can call on my page that prints the number of people registered?
First of all you should use count because of speed issues:
$sql = "SELECT COUNT(id) FROM persons";
To write a function that returns the number, you can do something like
function registredMemberCount ($connection)
{
$sql = "SELECT COUNT(id) FROM persons";
$result = mysqli_query($connection,$sql);
$rows = mysqli_fetch_row($result);
return $rows[0];
}
and call it with
registredMemberCount($connection);
require_once("connect.php");
function blah()
{
global $connection;
$sql = "SELECT COUNT(*) FROM persons";
if ($result=mysqli_query($connection, $sql)){
$row= mysqli_fetch_array($result);
$rowcount = $row[0];
mysqli_free_result($result);
}
return $rowcount;
}
echo blah();
Lets see this,
require('connect.php');
function total_num_users(){
$sql = "SELECT * FROM persons";
$result = mysqli_query($connection,$sql);
$count = mysqli_num_rows($result);
return $count;
}
And you can call and echo it like this.
echo total_num_users();

Count registered users in database

I would like to echo the number of people registered on my website
only the code that I have does not work, it gives me back that it can't be
converted to string. Also when I make it a function to call in my HTML I get error that $connection is undefined
require_once("connect.php");
$sql = "SELECT * FROM persons";
if ($result=mysqli_query($connection, $sql)){
$rowcount = mysqli_num_rows($result);
mysqli_free_result($result);
return $result;}
How do I get this in a function that I can call on my page that prints the number of people registered?
First of all you should use count because of speed issues:
$sql = "SELECT COUNT(id) FROM persons";
To write a function that returns the number, you can do something like
function registredMemberCount ($connection)
{
$sql = "SELECT COUNT(id) FROM persons";
$result = mysqli_query($connection,$sql);
$rows = mysqli_fetch_row($result);
return $rows[0];
}
and call it with
registredMemberCount($connection);
require_once("connect.php");
function blah()
{
global $connection;
$sql = "SELECT COUNT(*) FROM persons";
if ($result=mysqli_query($connection, $sql)){
$row= mysqli_fetch_array($result);
$rowcount = $row[0];
mysqli_free_result($result);
}
return $rowcount;
}
echo blah();
Lets see this,
require('connect.php');
function total_num_users(){
$sql = "SELECT * FROM persons";
$result = mysqli_query($connection,$sql);
$count = mysqli_num_rows($result);
return $count;
}
And you can call and echo it like this.
echo total_num_users();

Dynamic php page with database id

Hi I am trying to create a dynamic php page, that changes with the id of the database row.
But till now I got this, but it doesn't show anything :-(
Have tried to find some answers from previous questions, but found nothing that helped my problem.
Anybody got any nice and simple tips how to do this??
<?php
if (isset($_GET['id'])) {
$id = $mysqli->real_escape_string($_POST['id']);
$q = "SELECT * FROM `opskriftreg` WHERE `id` = '$term' LIMIT 1;";
$q = $mysqli->query($q);
if (mysqli_num_rows($q) > 0) {
$result = mysqli_fetch_assoc($q);
echo "<div class=\"article\">".
"<div class=\"title\">".$result['title']."</div>".
"<div class=\"body\">".$result['description']."</div>".
"<div class=\"desc\">"."Kort beskrivelse: ".$result['description']."</div>".
"<div class=\"ingredients\">"."ingredienser: ".$result['ingredients']."</div>".
"<div class=\"amount\">"."amount: ".$result['amount']."</div>".
"<div class=\"guidance\">"."guidance: ".$result['guidance']."</div>".
"<div class=\"hour\">"."hour: ".$result['hour']."</div>".
"<div class=\"minutes\">"."minutes: ".$result['minutes']."</div>".
"<div class=\"laktose\">"."laktose: ".$result['laktose']."</div>".
"<div class=\"okologisk\">"."okologisk: ".$result['okologisk']."</div>".
"</div>";
}
else {
/* Article not found */
}
}
?>
Would love to learn another way to do this, if there are any more simple ones.
I only included the php code, and not my database connection. It works, I have tested it :-)
You have this:
$id = $mysqli->real_escape_string($_POST['id']);
$q = "SELECT * FROM `opskriftreg` WHERE `id` = '$term' LIMIT 1;";
So I suppose your query simply returns zero rows since you are discarding $_POST['id'] and possibly searching by a literal '$term' string an empty string.
echo "SELECT * FROM `opskriftreg` WHERE `id` = '$term' LIMIT 1;";
Notice: Undefined variable: term in D:\tmp\test.php on line 3
SELECT * FROM opskriftreg WHERE id = '' LIMIT 1;
To make it worse, you only run the query if a third variable exists:
if (isset($_GET['id'])) {
You have complete mess with variables
Condition use $_GET['id']
after that you arr using $id = .... $_POST['id']
and inside SQL you have id = '$term'
Try to be consistent in using variables. I can assume that you need to update your code to
if (isset($_GET['id'])) {
$statement = $mysqli->prepare("SELECT * FROM `opskriftreg` WHERE `id` = ? LIMIT 1");
$statement->bind_param('i', $_GET['id']); //Replace ? above with our input, specifying ā€˜iā€™ meaning that we are passing one integer.
$statement->execute(); //Run the query
$result = $statement->get_result()->fetch_assoc();
// here goes your code
I haven't tested this, and I prefer PDO to mysqli, but it should get you closer.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$id = isset($_GET['id']) ? $_GET['id'] : (isset($_POST['id']) ? $_POST['id'] : false);
if ($id) {
$stmt = $mysqli->prepare("SELECT * FROM `opskriftreg` WHERE `id` = ? LIMIT 1");
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result()->fetch_array(MYSQLI_ASSOC);
if ($result) {
echo "<div class=\"article\">".
"<div class=\"title\">".$result['title']."</div>".
"<div class=\"body\">".$result['description']."</div>".
"<div class=\"desc\">"."Kort beskrivelse: ".$result['description']."</div>".
"<div class=\"ingredients\">"."ingredienser: ".$result['ingredients']."</div>".
"<div class=\"amount\">"."amount: ".$result['amount']."</div>".
"<div class=\"guidance\">"."guidance: ".$result['guidance']."</div>".
"<div class=\"hour\">"."hour: ".$result['hour']."</div>".
"<div class=\"minutes\">"."minutes: ".$result['minutes']."</div>".
"<div class=\"laktose\">"."laktose: ".$result['laktose']."</div>".
"<div class=\"okologisk\">"."okologisk: ".$result['okologisk']."</div>".
"</div>";
}
else {
/* Article not found */
}
}
<?php
if (isset($_GET['id'])) {
$id = $mysqli->real_escape_string($_GET['id']);
$sql = "SELECT * FROM `opskriftreg` WHERE `id` = ? LIMIT 1;";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('s', $id);
$stmt->execute();
$res = $stmt->get_result();
if (mysqli_num_rows($res) > 0) {
while ($result = $res->fetch_array(MYSQLI_ASSOC)) {
echo "<div class=\"article\">".
"<div class=\"title\">".$result['title']."</div>".
"<div class=\"body\">".$result['description']."</div>".
"<div class=\"desc\">"."Kort beskrivelse: ".$result['description']."</div>".
"<div class=\"ingredients\">"."ingredienser: ".$result['ingredients']."</div>".
"<div class=\"amount\">"."amount: ".$result['amount']."</div>".
"<div class=\"guidance\">"."guidance: ".$result['guidance']."</div>".
"<div class=\"hour\">"."hour: ".$result['hour']."</div>".
"<div class=\"minutes\">"."minutes: ".$result['minutes']."</div>".
"<div class=\"laktose\">"."laktose: ".$result['laktose']."</div>".
"<div class=\"okologisk\">"."okologisk: ".$result['okologisk']."</div>".
"</div>";
}
}
else {
/* Article not found */
}
}
?>
I've assumed you want to use the $_GET variable instead of $term or $_POST.

mysql select * or desired parameters in php function

I have a function which selects the * values from a particular table, I need to configure that if I pass * in parameter then it selects all or the selected parameters.
My function is as follows:
public function select($tablename){
$select = mysql_query("SELECT * FROM $tablename");
if(!$select){
echo "cant select table mane";
}
while ($row = mysql_fetch_array($select)){
print_r($row);
}
}
How can I pass the dynamic colum name in place of *
public function select($tablename,$column=NULL){
if($column == null) {
$select = mysql_query("SELECT * FROM $tablename");
} else {
$select = mysql_query("SELECT ".$column." FROM $tablename");
}
call it using select("abcd_table","id,name"); OR for all just select("abcd_table");
By Alex
Avoid mysql, it is deprecated. Use mysqli or PDO instead
That's even easier:
public function select( $tablename , $columnName = array() ){
$columnName = $columnName ? implode( ',' , $columnName ) : '*';
$select = mysql_query( 'SELECT ' . $columnName . ' FROM ' . $tableName );
if(!$select){
echo "cant select table mane";
}
while ($row = mysql_fetch_array($select)){
print_r($row);
}
}
Add one more parameter $columnname type array in your function.
public function select($tablename,$columnname = array()){
if(count($columnname)){
$columnname = implode(",",$columnname);
$select = mysql_query("SELECT {$columnname} FROM $tablename");
}
else{
$select = mysql_query("SELECT * FROM $tablename");
}
#code continue
}
Calling will be like below
To select all fields,
select($tablename);
To select some fields,
select($tablename,array('field','field2'));
Note: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
public function select($tablename, $selectrow){
$select = mysql_query("SELECT $selectrow FROM $tablename");
if(!$select){
echo "cant select table mane";
}
while ($row = mysql_fetch_array($select)){
print_r($row);
}
}
try this modified function
public function select($tablename,$array_fields){
if(!empty($array_fields)){
$fieldstr = implode(',',$array_fields);
$fieldstr = trim($fieldstr,',');
}else{
$fieldstr = '*';
}
$select = mysql_query("SELECT $fieldstr FROM $tablename");
if(!$select){
echo "cant select table mane";
}
while ($row = mysql_fetch_array($select)){
print_r($row);
}
}
public function select($tablename,$column="*"){
$select = mysql_query("SELECT $column FROM $tablename");
if(!$select){
echo "cant select table mane";
}
while ($row = mysql_fetch_array($select)){
print_r($row);
}
}
call like select("table","*") or
call like select("table","col1,col2")

My AJAX Application always returns "found"

I want to do a really simple task: check if an inserted id matches one found in a mysql database. If no match, an alert box will appear displaying an invalid id message. The problem that I have is my script ALWAYS returns found, even if the id does not exist in the database. Here is my script, any idea what is wrong?
PHP SCRIPT:
<?php
//connect to server....
$id = $_GET["id"];
$query = 'SELECT * from users where id = $id';
$result = mysql_query($query) or trigger_error(mysql_error().$query);
if (empty($result))
{
$response = "notfound";
echo $response;
}
else
{
$response = "found";
echo $response;
}
// Close connection to the database
mysql_close($con);
?>
You're using single quotes instead of double quotes on your query assignment. You must use double quotes or string concatenating.
$query = "SELECT * from users where id = $id";
OR
$query = 'SELECT * from users where id = ' . $id;
And you're also probably doing this in the insert, which is why it's always being 'found'.
Try:
$query = 'SELECT * from users where id = '.(int) $id;
$result = mysql_query($query) or trigger_error(mysql_error().$query);
$num_rows = mysql_num_rows($result);
if($num_rows > 0) {
echo "found";
}
else {
echo "not found";
}
I am no t familiar with php syntax but can you replace the if condition to be something like this
$result = mysql_query($query) or trigger_error(mysql_error().$query);
if($result)
{
$response = "found";
echo $response;
}
else
{
$response = "notfound";
echo $response;
}

Categories