cant retrieve data from mysql db with php - php

Sorry for my bad English,
I cant retrieve record from mysql database, there's no error messages and nothing showing. The things is I want to retrieve data on a page with session using external php file. My table has data on it, I've inserted it with phpmyadmin. Here's the code :
UPDATE
I only work on php file now, and here the konek.php
<?php
error_reporting(E_ALL);
$konek=new mysqli('localhost','root','zxcvbnm','jumat') or die(mysqli_connect_error);
$selek=mysqli_query($konek,'select * from kategori');
while($baris=mysqli_fetch_array($selek)){
echo $baris['kodekat'].' '.$baris['namakat'];
echo '<br>';
}
if($konek){
echo 'koneksi ok';
}
$konek->close();
?>
it only shows the 'koneksi ok' word, is the while not processed?
thank you in advance

What are you trying to do with this line:
if($i<=mysql_fetch_array($selek)){
If you are trying to compare with the no. of data use:
if($i<=mysql_num_rows($selek)){

From personal experience, I had trouble with this traditional MySQL api.
I'll share this small PDO tutorial that got me over this mess, hope this helps you as much as it helped me.
http://code.tutsplus.com/tutorials/php-database-access-are-you-doing-it-correctly--net-25338

Try this:
<?php
$konek=mysqli_connect("localhost","root","zxcvbnm","jumat") or die(mysqli_error());
$selek=mysqli_query($konek,"select kodekat,namakat from kategori");
$i=0;
if(mysqli_num_rows($selek) > 0 ){
while($baris = mysqli_fetch_array($selek)){
$id = $baris["kodekat"];
$nmk = $baris["namakat"];
echo"kode: ".$id."nama:".$nmk.'<br/>';
}
}else{
echo 'No items in database';
}
mysqli_close($konek);
?>

I know the problem.
Yhe problem is, my database is empty, I'm so forgetfull, :)
sorry for the inconvenience, I'm sorry I'm sorry,
the code is fine, all of them, :)
and all of answers above, I'm sure it works,
its my faults, that's my database was empty in the first place,

Related

How do i put a $ before a database tag

I try to make a website with an account system, but now i am stuck.
I already tries to do it with this code <?php echo "<p>$</p>"$userRow['username']; ?>
what i want to have is that there will be something like this $USERNAME and that has to transform into the thing that i put into my config file.
$daan0605 = ('CoOwner'); $mohagames205 = ('HeadCreator'); Sorry that i dont know all the terms yet,
but i hope you guys can help me :).
Variable variables is what you're looking for. In that case you would simply do,
<?php echo $$userRow['username']; ?>
So if $userRow['username'] outputs daan0605 and $daan0605 = 'CoOwner';, then the above statement would output CoOwner.

Can someone explain to me how this appending a table with ajax works?

So I am using Mottie's fork of the tablesorter plug in and it has been working well for me. However I have a couple pages that will have some large records of data that I need to deal with. So I assume the best way of going about this is using AJAX to fill the table. There is an example in the documentation but I do not know javascript or ajax very well so I am not sure exactly how it is working.
http://mottie.github.io/tablesorter/docs/example-ajax.html
A couple specific questions would be.
What would the code on assets/ajax-content.html look like? that is the piece I really wanted to know about. how it is actually getting the records and sending them back.
I also just don't fully understand how the
("#ajax-append").click(function()
is working. how is it receiving the 'html' parameter on the line
$.get("assets/ajax-content.html", function(html) {
Any help is really appreciated. thanks
From the examples, we can determine it's returning HTML. So your back-end script would want to do the same. From the example page, the tbody contains the following:
<tr>
<td>Bruce</td>
<td>Evans</td>
<td>22</td>
<td>$13.19</td>
<td>11%</td>
<td>Jan 18, 2007 9:12 AM</td>
</tr>
So when we click the link (Append new table data) in the example, we append more Table Rows to the Table Body. We can only hope that what it wants is more of the same. An example could be:
<?php
// connect to DB
// run query
// get recordset
// output recordset in Table Row format
while($row = $sql->fetch_assoc($results)){
echo "<tr>\r\n";
echo "\t<td>{$row['fname']}</td>\r\n";
echo "\t<td>{$row['lname']}</td>\r\n";
echo "\t<td>{$row['age']}</td>\r\n";
echo "\t<td>{$row['total']}</td>\r\n";
echo "\t<td>{$row['discount']}</td>\r\n";
echo "\t<td>{$row['date']}</td>\r\n";
echo "</tr>\r\n";
}
$sql->close();
?>

Need help saving the answer from a Select into Database

Hi there;
I need some help; I need to save the answer from a select into my database. I do not know where I went wrong. Here is some details regarding my code.
I have a database named 'lib'
The table is named 'games'
I need to save the following into the table; 1) Game (all the words) and 2) Type
It is for a tournament website. So that I can make it available on the LAN via XAMPP.
Here is my code.
http://pastebin.com/iP7MCMzJ
Thanks in advance!
UPDATE:
Thanks for all the help! I have found and fixed my problem! In my database the VARCHAR was limited to 50 characters. I have fixed that. Now all is good :)
Thanks once again!
Well, you're not showing any code but this is how I'd expect it to work based on your description:
Your HTML will want to look something like:
<?php echo '<option name="game" value="' . htmlentities($game) . '">' . $row['game'] . '</option>'; ?>
Notice the name attribute? Also, I changed the way the HTML was written as yours was very tough to read.
PHP
if(isset($_POST['game']) && isset($_POST['type'])){
$game = $_POST['game'];
$type = $_POST['type'];
// PLEASE CLEAN YOUR INPUT PRIOR TO INSERTING THIS DATA!
Insert into games (Game, Type) VALUES ($game, $type)
}
else{
echo( "error getting info");
}

Getting value from session PHP

I am not sure why the variable username is not being returned in the session. When the user logs in, I start the session:
$username = trim($_POST['username']);
if(!isset($_SESSION)){ session_start(); }
$_SESSION[$this->GetLoginSessionVar()] = $username;
On the user's welcome page, when I run the echo command, I see the proper variable being returned. But I'm not sure why the return statement isn't working. I have the following in my PHP file:
function UserName()
{
return isset($_SESSION['name_of_user']) ? $_SESSION['name_of_user'] : "Unknown User" ;
//echo $_SESSION['name_of_user'];
}
In my html, I have:
Welcome back <?PHP $fgmembersite->UserName(); ?>!
I also checked the session ID, and it's also being generated properly.
Can you please help me understand what I'm doing wrong?
Is fgmembersite an object and have it the function called UserName ?
If yes, you simply miss an echo
<?PHP echo $fgmembersite->UserName(); ?>
You must add echo or print so should look like this;
<?PHP echo $fgmembersite->UserName(); ?>
You need to print out your variable. Use
Echo or print
Possibly you should add output:
<?php print $fgmembersite->UserName(); ?>
If you are using the script I think you are using, you need to look through fg_membersite.php at the line that says:
function CheckLoginInDB($username,$password)
whithin that line you should have a MySQL statement:
$qry = "SELECT etc...
When I tried to add UserAvatar I was able to do that by adding it to that MySQL string.
On a side note, I too am having trouble with adding UserName, and for the life of me I can't figure out why it would work any different than my previous workaround, yet somehow it is, but I am still convinced something in that file will do the trick eventually.
Edited:
Ok i got it, just do this:
echo $fgmembersite->UserName($username);
The username will pop right out. I have no idea why, i don't know enough php to explain it, but i can only assume this will get you going.

delete a row from my sql table

I'm still new to php and working my way around it but i'm stuck at the following piece:
code for deleting a row in my table
i have a link directing towards this piece of my script. i run through the first half just fine but when i press on submit and try to execute my delete query it won't go to my second if statement let alone get to the delete query.
$pgd is the page id
my hunch is there is problem with the action in the form i'm building after my while statement
forgive me for the wierd formatting of my msg but its 2am and very tired, i promise to format my questions in the future better! any help is appreciated
edit: ok other then the obvious mistake of missing method=post #.#;
edit:
hey everyone,
first of all, i'd like to thank everyone for their response.
i just started coding in php last weekend so forgive my messy codes. the code is still running locally and my main goal was to finish the functions and then work on securing my code.
now back to the issue, i'm sorry if i was vague about my problem. i'll try to reiterate it.
my issue isn´t selecting an item i want to delete, the issue is that it won´t get to the 2nd if statement.
Re-edit:
this time with my current code:
if($_GET['delete'] == "y")
{
//content hier verwijderen
$sqlcont1="SELECT * FROM content where id ='".$_GET['id']."'";
echo $sqlcont1;
$resultcont1 = mysql_query($sqlcont1) or die (include 'oops.php');
while($rowcont1= mysql_fetch_array($resultcont1)){
echo '<form class="niceforms" action="?pg='.$pgd.'&delete=y&remove=y&id='.$_GET['id'].'" method="post">';
echo '<h1>'.$rowcont1['Titel'].'</h1>';
echo '<p>'.$rowcont1['Content'].'</p>';
echo '<input type="submit" value="Delete article">';
echo '</form>';
}
if($_GET['remove']=="y"){
echo 'rararara';
$id=$_GET['id'];
$sqlrem="DELETE FROM content WHERE id="$id;
echo $sqlrem;
mysql_query($sqlrem);
}
}
echoing $sqlrem gives me the following now:
DELETE FROM content WHERE id=8
that being my current code, i get in to the second IF statement but now to get it to delete!
#everyone:
ok maybe thinking out loud or following my steps worked but the code works, i know its very messy and it needs fine tuning. i'd like to thank everyone for their help and feedback. i'm liking this and you'll probably see me alot more often with nubby questions and messy codes with no escapes :(
First of all, you have SQL injection vulnerability in your script. Anyone can add some string that will be attached to your query, possibly altering it in a way that can make almost anything with the data from your database.
Escape your values with one of anti-SQL-injection methods. Read more for example on php.net/manual/en/function.mysql-query.php
To the point...
Your deletion code will be executed only if you invoke URL with two params (remove and delete set to y. That means your URL should look similar to something.php?delete=y&remove=y. Maybe you just did not spot it.
Please give details about any errors that occured and tell me whether the above mentioned solution helped.
mysql_fetch_array() returns an array
your while statement acts as an if, and does not iterate thru the array returned as you think it does
you need something like
$all_rows = mysql_fetch_array($result);
foreach ($all_rows as $row) {
$sql = "delete from table where id = " . $row['id'];
}
It looks to me like you're mixing two forms together here: you're wanting to see if you went to the delete row form (the first few lines), and you're trying to present the delete row form (the while loop.) I would break these two things apart. Have a page that simply displays your forms for row deletes, and another page that processes those requests. And another page that brings you to the delete rows page.
For now, just echo all the values you're expecting to receive in $_GET[] and see if they are what you expect them to be.
You have a lot of problems in that script alone, so just to make things easier (considering you uploaded a pic), put an
echo $sqlrem;
in your second if statement, see if the query is displayed. If not, it means it doesn't even get to that part of code, if it gets displayed, copy it and run it in phpmyadmin. That should output a more coherent error message. Tell us what that is and we'll work it through.
I also noticed that your DELETE SQL query might have an issue. If your $pgd' id is a integer, you shouldn't include the ' single quote, that is for string only.
**Correction**
$sqlrem = "DELETE FROM content WHERE id = " . controw1['id'];
EDIT
Anyway, just to help out everyone, I typed out his code for easier viewing.
I think his error is $rowcont1['Tilel'] --> that might caused PHP to have an error because that column doesn't exist. I assumed, it should be `Title' causing an typo error.
if(_$GET['delete'] == "y") {
$sqlcont1 = "SELECT * FROM content where id ='" . $_GET['id'] . "'";
$resultcont1 = mysql_query($sqlcont1) or die (include 'oops.php');
while ($rowcont1 = mysql_fetch_array($resultcont1)) {
echo '<form class = "niceforms" action = "?pg=' .$pgd . '&delete=y&remove=y">';
echo '<h1>' . $rowcont1['Title'] . '<h1>'; // <-- error here
echo '<p>' . $rowcont1['Content'] . '</p>';
echo '<input type = "submit" value = "Delete article">';
echo '</form>';
}
if ($_GET['remove'] == "y"){
$sqlrem = "DELETE FROM content WHERE id = " . $rowcont1['id'];
mysql_query ($sqlrem);
}
}

Categories