SELECT * insted of typing all columns

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

SELECT * insted of typing all columns

Post by ianhull »

Hi Guys,

I'm back for more help.

I have always used SELECT Username, Password FROM table but I have loads of columns in this project and I didn't really want to type out each one so I thought of using SELECT * instead but it does not work.

Can anyone see why in my code below?

Thanks.

Code: Select all

<?php session_start();
include_once("includes/globals.php");

$email = $_POST['email'];
$password = $_POST['password'];

$live = "Active";

if(empty($email)){
include_once("error_pages/login_error.php");
exit();
}
elseif(empty($password)){
include_once("error_pages/login_error.php");
exit();
}
include_once("includes/connect.php");

$sql = "SELECT * FROM users WHERE email='$email' AND password='$password' AND status='$live'";
$r = mysql_query($sql);
if(!$r) {
$err=mysql_error();
print $err;
exit();
}
if(mysql_affected_rows()==0){
include_once("error_pages/index_error.php");
exit();
}
else{
}
while ($line = mysql_fetch_array($r))    
{
extract($line);
}

$_SESSION['mycompanyname'] = $companyname;
$_SESSION['myfirstname'] = $firstname;
$_SESSION['mylastname'] = $lastname;
$_SESSION['myemail'] = $email;
$_SESSION['mypassword'] = $password;
$_SESSION['mysite'] = $site;
$_SESSION['mytitle'] = $title;

echo $_SESSION['mycompanyname'];


?>
User avatar
$phpNut
Forum Commoner
Posts: 40
Joined: Tue May 09, 2006 5:13 pm

Post by $phpNut »

on this line:

Code: Select all

if(mysql_affected_rows()==0){
your missing the resource

Code: Select all

if(mysql_affected_rows($r)==0){
...
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Thanks, I tried that but it did not work.

That has always worked before, I really just wanted to be lazy and use * instead of typing out 28 columns in my SELECT statement.

Nevermind, Thanks anyway.
User avatar
$phpNut
Forum Commoner
Posts: 40
Joined: Tue May 09, 2006 5:13 pm

Post by $phpNut »

What error are you actually getting?
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

No errors, just trying to work around the SELECT.

I have to type username, password, email, company, site, photo, etc etc I was hoping I could just use SELECT * and have the the variables and values available.

I have started typing now so I will just use my old way.

Thanks
User avatar
$phpNut
Forum Commoner
Posts: 40
Joined: Tue May 09, 2006 5:13 pm

Post by $phpNut »

Oh so thats what you wanted,

try changing this,

Code: Select all

while ($line = mysql_fetch_array($r))   
{
extract($line);
}

$_SESSION['mycompanyname'] = $companyname;
$_SESSION['myfirstname'] = $firstname;
$_SESSION['mylastname'] = $lastname;
$_SESSION['myemail'] = $email;
$_SESSION['mypassword'] = $password;
$_SESSION['mysite'] = $site;
$_SESSION['mytitle'] = $title;

echo $_SESSION['mycompanyname'];
to this

Code: Select all

while ($row = mysql_fetch_object($r)) {
    $_SESSION['mycompanyname'] = $row->companyname;
    $_SESSION['myfirstname'] = $row->firstname;
    $_SESSION['mylastname'] = $row->lastname;
    $_SESSION['myemail'] = $row->email;
    $_SESSION['mypassword'] = $row->password;
    $_SESSION['mysite'] = $row->site;
    $_SESSION['mytitle'] = $row->title;
}

echo $_SESSION['mycompanyname'];
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Silly :) mysql_affected_rows() won't return anything if you're not running an update or a delete. Use mysql_num_rows() instead ;)
Post Reply